Chewee133
Chewee133

Reputation: 65

The method 'BlocProvider' isn't defined

I'm trying to to use Bloc but my BlocProvider return an error:

The method 'BlocProvider' isn't defined for the type '_MyAppState'.
Try correcting the name to the name of an existing method, or defining a method named 'BlocProvider'.

I cant find anywhere about solving this issue.

Here's the code i'm working with

import 'package:app_13/post_cubit.dart';
import 'package:app_13/post_view.dart';
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocProvider<PostsCubit> (
        create: (context) => PostsCubit(),
        child: PostsView(),
      ),
    );
  }
}

And added flutter_bloc in pub.yaml

  bloc: ^8.1.0

What am i doing wrong? Why is BlocProvider not recognized?

Thanks in advance for the help.

Upvotes: 3

Views: 5197

Answers (3)

user20506205
user20506205

Reputation:

import 'package:flutter_bloc/flutter_bloc.dart';

use the above in the import section and your error should be gone. I hope it helps :)

Upvotes: 1

Noman Ahmad
Noman Ahmad

Reputation: 31

For this problem use "import 'package:flutter_bloc/flutter_bloc.dart';"

Upvotes: 3

Jan-Stepien
Jan-Stepien

Reputation: 523

Try using flutter_bloc package instead

It has the additional classes such as BlocProvider

Upvotes: 7

Related Questions