我对在flutter中使用提供程序有疑问

时间:2020-08-25 12:03:56

标签: flutter

您好,我正在研究有关供应商的信息。

我只是在学习时移动了示例代码,但是发生以下错误。

库是否已更改或发生错误?

enter image description here

class BankAccount with ChangeNotifier {
  int _balance = 0;

  int getBalance() => _balance;

  void increment(int value) {
    _balance += value;
    notifyListeners(); //must be inserted
  }

  void decrement(int value) {
    _balance -= value;
    notifyListeners(); //must be inserted
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<BankAccount>(builder: (_) => BankAccount(),),//This is a problem
        Provider<String>.value(value: "Park")
      ],
      child: MaterialApp(
        title: "Provider Test",
        home: HomePage(),
      ),
    );
  }
}
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    BankAccount bankAccount = Provider.of<BankAccount>(context);
    String name = Provider.of<String>(context);
  }
}

1 个答案:

答案 0 :(得分:0)

Refer here

ChangeNotifierProvider(
  create: (_) => BankAccount(),
)