Flutter BLoC (v8): How to Fetch Data From an API? - 2022 Guide
How to fetch the data from the API by using BLoC Architecture?

Search for a command to run...
How to fetch the data from the API by using BLoC Architecture?

Hey, Nice article ! I like it, BTW when I run the compiled app, I have an issue :(
. The following _CastError was thrown building _BodyBuilder: type 'Null' is not a subtype of type 'JokeBloc' in type cast
The relevant error-causing widget was Scaffold package:test_bloc/…/home/home.dart:15 When the exception was thrown, this was the stack
Any idea what it can be ? Because I'm really interested to use this pattern for our future apps
Excellent please can you make a video with flutter block with a like button? in flutter
Thanks 😀
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders ↔ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts


home.dart file and paste the below code.class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('The Joke App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const ExpansionTile(
title: Text(
"Joke",
textAlign: TextAlign.center,
),
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text(
"",
style: TextStyle(
fontSize: 20,
),
textAlign: TextAlign.center,
),
),
],
),
ElevatedButton(
onPressed: () {
//TODO Load New Joke
},
child: const Text('Load New Joke'),
),
],
),
),
);
}
}

main.dart.class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.grey[900],
backgroundColor: Colors.grey[900],
scaffoldBackgroundColor: Colors.grey[900],
buttonTheme: ButtonThemeData(
buttonColor: Colors.grey[900],
textTheme: ButtonTextTheme.primary,
),
),
home: const Home(),
);
}
}
dependencies:
flutter_bloc: ^8.0.1
http: ^0.13.4
equatable: ^2.0.3

lib/data/model/joke_model.dart and paste the following code.http package to get the API from the internet.Create a file called joke_repository.dart in the lib/data/repository folder and paste the code below into it.
class JokeRepository {
final String _baseUrl = "https://v2.jokeapi.dev/joke/Any";
Future<JokeModel> getJoke() async {
final response = await http.get(Uri.parse(_baseUrl));
if (response.statusCode == 200) {
return jokeModelFromJson(response.body);
} else {
throw Exception("Failed to load joke");
}
}
}
main.dart file.class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: (....),
home: RepositoryProvider(
create: (context) => JokeRepository(),
child: const Home(),
),
);
}
}
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => JokeBloc(
RepositoryProvider.of<JokeRepository>(context),
)..add(LoadJokeEvent()),
child: Scaffold(
....
)
)
}

