为什么在 flutter 中使用 provider 时 class 的构造函数没有被执行?

Why the constructor of class isn't executed when using provider in flutter?

void main() {
  MainStream.init();
  runApp(
      MultiProvider(
          providers: [
            Provider(
              create: (context) => Test(context),
            ),
          ],
          child: MyApp()));
}

class Test {
  Test(BuildContext context) {
    print("Test");
  }
}

在此测试代码中,我希望在我的应用程序启动时打印出 "Test" 但它没有。我做错了什么?我看到了像这样初始化提供程序的示例。

根据提供程序文档,创建回调是延迟加载的,因此这是预期的行为。如果您通过了 "lazy: false",它应该会按您预期的那样工作:

Provider(
    create: (context) => Test(context),
    lazy: false
),