BLoC模式的Flutter Firestore错误 [英] Flutter Firestore error with BLoC pattern

查看:53
本文介绍了BLoC模式的Flutter Firestore错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扑朔迷离的新手有很多东西,现在才开始弄清楚它是BLoC模式,现在我遇到了一个问题 我不明白如何解决该错误,似乎一切都写正确了 此处是所有BLoC的通用接口

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

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;
  }
}

这是我使用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(),
  ),
);

这篇关于BLoC模式的Flutter Firestore错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆