我尝试使用Bloc,但我的BlocProvider返回错误:
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'.
我找不到解决这个问题的方法。
这是我正在使用的代码
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(),
),
);
}
}
并在pub.yaml中添加flutter_bloc
bloc: ^8.1.0
我做错了什么?为什么无法识别BlocProvider?
先谢谢你的帮助。
2条答案
按热度按时间x3naxklr1#
尝试改用flutter_bloc包
它具有
BlocProvider
等附加类bxjv4tth2#
对于此问题,请使用“导入”包:flutter_bloc/flutter_bloc. dart“;“