flutter中的provider报错

class MeetingListModel with ChangeNotifier {
  int _count = 0;
  int get count => _count;
  void add() {
    _count++;
    notifyListeners();
  }
}

class MeetingList extends StatefulWidget {
  @override
  _MeetingListState createState() => _MeetingListState();
}

class _MeetingListState extends State<MeetingList> {
  int type = 0;

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (_) => MeetingListModel(),
          )
        ],
        child: Scaffold(
          body: Center(
            child: Column(children: [
              GestureDetector(
                  onTap: () {
                    context.watch<MeetingListModel>().add();
                  },
                  child: Text("会议列表")),
              Text(context.watch<MeetingListModel>().count.toString()),
            ]),
          ),
        ));
  }
}

这样写为什么会报错,按照推荐的写法

Widget build(BuildContext context) {
 return Provider<Example>(
create: (_) => Example(),
 builder: (context) {
 return Text(context.watch<Example>()),
}),}

也同样报错,求解啊

阅读 121
评论
    1 个回答
    • 137
    context.watch<MeetingListModel>().add();

    watch改成read试试

      相似问题
      推荐文章