在构建期间调用 Flutter 提供程序抛出错误 setState() 或 markNeedsBuild()

Flutter provider throwing error setState() or markNeedsBuild() called during build

我正在尝试学习如何使用 flutter 团队提供的 provider package, version 4.0+. I have copy-pasted solution 和 运行 在我的模拟器上使用它。问题是,当我第一次 运行 它(冷启动)时,我收到以下错误消息:

当我刷新应用程序时(意思是,当我在重新构建应用程序的 VS Code 中单击刷新箭头时),它突然开始工作。这意味着,该问题仅在第一次(冷)启动期间出现。

我真的很想学习如何使用这个包;但由于未知原因,它抛出了这个错误。正如我阅读其他文章一样,人们试图在构建方法期间修改实现 ChangeNotifier 的 Class 的值;但是在这种情况下,情况不应如此。

如能提供有关此事的任何帮助,我们将不胜感激。

P.S。这是我正在使用的实际源代码:

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    // Provide the model to all widgets within the app. We're using
    // ChangeNotifierProvider because that's a simple way to rebuild
    // widgets when a model changes. We could also just use
    // Provider, but then we would have to listen to Counter ourselves.
    //
    // Read Provider's docs to learn about all the available providers.
    ChangeNotifierProvider(
      // Initialize the model in the builder. That way, Provider
      // can own Counter's lifecycle, making sure to call `dispose`
      // when not needed anymore.
      create: (context) => Counter(),
      child: MyApp(),
    ),
  );
}

/// Simplest possible model, with just one field.
///
/// [ChangeNotifier] is a class in `flutter:foundation`. [Counter] does
/// _not_ depend on Provider.
class Counter with ChangeNotifier {
  int value = 0;

  void increment() {
    value += 1;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            // Consumer looks for an ancestor Provider widget
            // and retrieves its model (Counter, in this case).
            // Then it uses that model to build widgets, and will trigger
            // rebuilds if the model is updated.
            Consumer<Counter>(
              builder: (context, counter, child) => Text(
                '${counter.value}',
                style: Theme.of(context).textTheme.display1,
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        // Provider.of is another way to access the model object held
        // by an ancestor Provider. By default, even this listens to
        // changes in the model, and rebuilds the whole encompassing widget
        // when notified.
        //
        // By using `listen: false` below, we are disabling that
        // behavior. We are only calling a function here, and so we don't care
        // about the current value. Without `listen: false`, we'd be rebuilding
        // the whole MyHomePage whenever Counter notifies listeners.
        onPressed: () =>
            Provider.of<Counter>(context, listen: false).increment(),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

它在 stable v1.12.13+hotfix.7 上运行良好,所以可能是最新版本的错误。

我对您的建议是使用稳定版本,如果您尝试使用 dev 或 master 并发现错误,请在此处报告:https://github.com/flutter/flutter/issues