I was trying to fetch data using API and Bloc builder in a flutter. But I am getting an error while running the bloc. Here is my code. I have also added an error screenshot below the code. Thanks.
Main.dart
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:footballplayers/services/api_services.dart';
import 'homeScreen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MultiRepositoryProvider(
providers: [
RepositoryProvider(
create: (context) => ApiServices(),
),
],
child: HomeScreen(),
),
);
}
}
HomeScreen.dart
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:footballplayers/bloc/recipe_bloc.dart';
import 'package:footballplayers/models/recipe_model.dart';
import 'package:footballplayers/services/api_services.dart';
class HomeScreen extends StatefulWidget {
HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) =>
RecipeBloc(RepositoryProvider.of<ApiServices>(context))
..add(GetRecipeEvent()),
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.restaurant_menu,
color: Colors.black,
),
Text(
'FoodRecipe',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
)
],
),
),
body: BlocBuilder<RecipeBloc, RecipeState>(
builder: (context, state) {
if (state is RecipeLoading) {
return Center(
child: CircularProgressIndicator(),
);
}
if (state is RecipeLoaded) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
padding: EdgeInsets.all(10),
child: ListView.builder(
shrinkWrap: true,
itemCount: state.recipeData.length,
itemBuilder: (context, index) {
return recipeCard(
imgUrl: state.recipeData[index].imgUrl,
recipeName: state.recipeData[index].name,
rating: state.recipeData[index].rating,
totalTime: state.recipeData[index].totalTime,
);
}),
);
} else {
throw Exception('Something went wrong');
}
},
),
),
);
}
}
class recipeCard extends StatelessWidget {
String imgUrl;
String recipeName;
double rating;
String totalTime;
recipeCard({
Key? key,
required this.imgUrl,
required this.recipeName,
required this.rating,
required this.totalTime,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
child: Column(children: [
ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image.network(
imgUrl,
fit: BoxFit.cover,
)),
Text(
recipeName,
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Color.fromARGB(255, 18, 224, 63),
),
child: Text(
rating.toString(),
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
),
),
),
Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Color.fromARGB(255, 70, 18, 224),
),
child: Text(
totalTime,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 15,
),
),
),
],
),
),
]),
);
}
}
ApiServices.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:footballplayers/models/recipe_model.dart';
import 'package:http/http.dart' as http;
class ApiServices {
List<RecipeModel> recipeList = [];
Future<List<RecipeModel>> getRecipe() async {
final uri = Uri.https(
'yummly2.p.rapidapi.com', '/feeds/list', {'limit': '24', 'start': '0'});
final response = await http.get(
uri,
headers: {
'X-RapidAPI-Key': '80cb4acef6mshf3b62df4bcdc92ap1b8e20jsna293a3b01f0f',
'X-RapidAPI-Host': 'yummly2.p.rapidapi.com',
},
);
final data = jsonDecode(response.body);
final values = data['feed'];
for (int i = 0; i < values.length; i++) {
if (values[i]['content']['details']['name'] != null) {
RecipeModel recipeModel = RecipeModel(
name: values[i]['content']['details']['name'] as String,
imgUrl: values[i]['content']['details']['images'][0]['hostedLargeUrl']
as String,
totalTime: values[i]['content']['details']['totalTime'] as String,
rating: values[i]['content']['details']['rating'] as double,
);
recipeList.add(recipeModel);
}
}
return recipeList;
}
}
**RecipeModel.dart**
// ignore_for_file: public_member_api_docs, sort_constructors_first
class RecipeModel {
String name;
String imgUrl;
String totalTime;
double rating;
RecipeModel({
required this.name,
required this.imgUrl,
required this.totalTime,
required this.rating,
});
factory RecipeModel.fromJson(Map<String, dynamic> json) {
return RecipeModel(
name: json['name'],
imgUrl: json['images'][0]['hostedLargeUrl'],
totalTime: json['totalTime'],
rating: json['rating'],
);
}
@override
String toString() {
return 'RecipeModel(name: $name, imgUrl: $imgUrl, totalTime: $totalTime, rating: $rating)';
}
}
RecipeBloc.dart
import 'package:bloc/bloc.dart';
import 'package:footballplayers/models/recipe_model.dart';
import 'package:footballplayers/services/api_services.dart';
import 'package:meta/meta.dart';
part 'recipe_event.dart';
part 'recipe_state.dart';
class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
final ApiServices apiServices;
List<RecipeModel> recipeD = [];
RecipeBloc(this.apiServices) : super(RecipeInitial()) {
on<GetRecipeEvent>((event, emit) async {
emit(RecipeLoading());
final activity = await apiServices.getRecipe();
recipeD = apiServices.recipeList;
emit(RecipeLoaded(recipeData: recipeD));
});
}
}
RecipeBlocEvent.dart
part of 'recipe_bloc.dart';
@immutable
abstract class RecipeEvent {}
class GetRecipeEvent extends RecipeEvent {}
RecipeBlocState.dart
// ignore_for_file: public_member_api_docs, sort_constructors_first
part of 'recipe_bloc.dart';
abstract class RecipeState {}
class RecipeInitial extends RecipeState {}
class RecipeLoading extends RecipeState {}
class RecipeLoaded extends RecipeState {
List<RecipeModel> recipeData;
RecipeLoaded({
required this.recipeData,
});
@override
String toString() => 'RecipeLoaded(recipeData: $recipeData)';
}
════════ Exception caught by widgets library ═══════════════════════════════════
The following _Exception was thrown building BlocBuilder<RecipeBloc, RecipeState>(dirty, dependencies: [_InheritedProviderScope<RecipeBloc?>], state: _BlocBuilderBaseState<RecipeBloc, RecipeState>#d4a99):
Exception: Something went wrong
The relevant error-causing widget was
BlocBuilder<RecipeBloc, RecipeState>
package:footballplayers/homeScreen.dart:45
When the exception was thrown, this was the stack
#0 _HomeScreenState.build.<anonymous closure>
package:footballplayers/homeScreen.dart:71
#1 BlocBuilder.build
package:flutter_bloc/src/bloc_builder.dart:90
#2 _BlocBuilderBaseState.build
package:flutter_bloc/src/bloc_builder.dart:166
#3 StatefulElement.build
package:flutter/…/widgets/framework.dart:4919
#4 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4806
#5 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4977
#6 Element.rebuild
package:flutter/…/widgets/framework.dart:4529
#7 ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4787
#8 StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4968
#9 ComponentElement.mount
package:flutter/…/widgets/framework.dart:4781
... Normal element mounting (19 frames)
#28 Element.inflateWidget
package:flutter/…/widgets/framework.dart:3817
#29 MultiChildRenderObjectElement.inflateWidget
package:flutter/…/widgets/framework.dart:6350
#30 MultiChildRenderObjectElement.mount
package:flutter/…/widgets/framework.dart:6362
... Normal element mounting (91 frames)
#121 _InheritedProviderScopeElement.mount
package:provider/src/inherited_provider.dart:411
... Normal element mounting (7 frames)
#128 SingleChildWidgetElementMixin.mount
package:nested/nested.dart:222
... Normal element mounting (7 frames)
#135 SingleChildWidgetElementMixin.mount
package:nested/nested.dart:222
... Normal element mounting (15 frames)
#150 _InheritedProviderScopeElement.mount
package:provider/src/inherited_provider.dart:411
... Normal element mounting (7 frames)
#157 SingleChildWidgetElementMixin.mount
package:nested/nested.dart:222
... Normal element mounting (7 frames)
#164 _NestedHookElement.mount
package:nested/nested.dart:187
... Normal element mounting (7 frames)
#171 SingleChildWidgetElementMixin.mount
package:nested/nested.dart:222
... Normal element mounting (24 frames)
[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: NoSuchMethodError: The method
'[]' was called on null.
Receiver: null
Tried calling: []("name")
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1 ApiServices.getRecipe
package:footballplayers/services/api_services.dart:24
<asynchronous suspension>
#2 new RecipeBloc.<anonymous closure>
package:footballplayers/bloc/recipe_bloc.dart:15
<asynchronous suspension>
#3 Bloc.on.<anonymous closure>.handleEvent
package:bloc/src/bloc.dart:211
<asynchronous suspension>
Thanks in advance for stackoverflow community.

