Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person

Categories

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>()),
}),}

也同样报错,求解啊


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
584 views
Welcome To Ask or Share your Answers For Others

1 Answer

context.watch<MeetingListModel>().add();

watch改成read试试


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...