简体   繁体   English

BLoC模式的Flutter Firestore错误

[英]Flutter Firestore error with BLoC pattern

A newbie in flutter has a lot of stuff that is just starting to figure out now it's BLoC pattern and now I ran into a problem I can not understand how to fix this error, seems to have written everything correctly Here generic Interface for all BLoCs 扑扑的新手有很多东西,现在才开始弄清楚它是BLoC模式,现在我遇到一个问题, 我不明白如何解决此错误,似乎已经正确编写了所有内容 这里是所有BLoC的通用接口

abstract class BlocBase {
  void dispose();
}

class BlocProvider<T extends BlocBase> extends StatefulWidget {
  BlocProvider({
    Key key,
    @required this.child,
    @required this.bloc,
  }) : super(key: key);

  final T bloc;
  final Widget child;

  @override
  _BlocProviderState<T> createState() => _BlocProviderState<T>();

  static T of<T extends BlocBase>(BuildContext context) {
    final type = _typeOf<BlocProvider<T>>();
    BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
    return provider.bloc;
  }

  static Type _typeOf<T>() => T;
}

class _BlocProviderState<T> extends State<BlocProvider<BlocBase>> {
  @override 
  void dispose() {
    widget.bloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

Here is the second file in which I use BLoC and where it gives an error Here I use function validateAndCreateData through which I add Tickets 这是我在其中使用BLoC的第二个文件,并且在其中出现错误。 在这里,我使用功能validateAndCreateData来添加工单

@override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<TicketsBloc>(context);
    return Scaffold(
        drawer: MyDrawer(),
        appBar: AppBar(
            title: Text('Sports'),
            backgroundColor: Colors.blueGrey[900],
            // automaticallyImplyLeading: false,
            actions: <Widget>[
              IconButton(
                  icon: Icon(Icons.share),
                  tooltip: 'Share',
                  onPressed: () {
                    Navigator.of(context).pushNamed('/second_screen');
                  }),
              IconButton(
                  icon: Icon(Icons.account_circle),
                  tooltip: 'Your account',
                  onPressed: () {
                    Navigator.of(context)
                        .pushReplacementNamed('/account_screen');
                  }),
              IconButton(
                icon: Icon(Icons.add),
                tooltip: 'Add Tickets',
                onPressed: () => validateAndCreateData(bloc),
              )
            ]),
        body: MyTab(),
    );
  }
void validateAndCreateData(TicketsBloc bloc) async {
      bloc.createData(description, image, name, price);

  }

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Your error mean you don't have access to the bloc. 您的错误意味着您无权访问该集团。 You must wrap your app with the provider. 您必须使用提供程序包装您的应用程序。 If not you cannot inherited from this. 如果没有,您将无法从中继承。

return BlocProvider(
    child: MaterialApp(
      title: 'My App',
      home: HomeScreen(),
  ),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM