flutter 未定义方法“BlocProvider”

9rnv2umw  于 2023-01-31  发布在  Flutter
关注(0)|答案(2)|浏览(138)

我尝试使用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?
先谢谢你的帮助。

x3naxklr

x3naxklr1#

尝试改用flutter_bloc
它具有BlocProvider等附加类

bxjv4tth

bxjv4tth2#

对于此问题,请使用“导入”包:flutter_bloc/flutter_bloc. dart“;“

相关问题