如何定义参数并将其从 ChangeNotifierProvider 传递给子项 class
How to define and pass a parameter from ChangeNotifierProvider to a child class
我的代码需要一些帮助。
我重复一遍,我想在行 9
和行 12
上解决这个问题,我在其中调用了同一个构造函数两次。是否有可能只调用一次并将 LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version)
的调用存储到参数中并将其传递给 MainPage
构造函数?
如果答案是肯定的,那怎么可能呢?
这是我的代码:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
SharedPreferences.getInstance().then((prefs) async {
runApp(
ChangeNotifierProvider(
create: (_) => LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version),
child: MainPage(
initialRoute: isLogged ? '/home' : '/',
authService: LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version),
),
),
);
});
}
class MainPage extends StatefulWidget {
final String initialRoute;
final LocalAuthService authService;
const MainPage(
{Key? key, required this.initialRoute, required this.authService})
: super(key: key);
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
final String appName = 'My App';
int currentIndex = 0;
late final screens = [
HomeScreen(authService: widget.authService),
SecondScreen(),
ThirdScreen(),
FourthScreen()
];
}
当我 运行 应用程序时,显然是因为构造函数被调用了两次,控制台看起来像这样:
感谢阅读本文!
问题在于,当您调用 class 的构造函数时 - 每次都会创建新的唯一对象。因此 ChangeNotifierProvider 的主要 use-case 是创建一些在整个应用程序的上下文中可用的单例对象。这意味着 - 您不必为 MainPage 放置两次,而是使用 Selector 或 Consumer 访问它:
....
late final screens = [
Consumer<LoginAuthService>(builder:(context, service, _) => HomeScreen(authService: service )),
SecondScreen(),
ThirdScreen(),
FourthScreen()];
.....
您可以从 MainPage 的构造函数中删除参数
我的代码需要一些帮助。
我重复一遍,我想在行 9
和行 12
上解决这个问题,我在其中调用了同一个构造函数两次。是否有可能只调用一次并将 LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version)
的调用存储到参数中并将其传递给 MainPage
构造函数?
如果答案是肯定的,那怎么可能呢?
这是我的代码:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
SharedPreferences.getInstance().then((prefs) async {
runApp(
ChangeNotifierProvider(
create: (_) => LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version),
child: MainPage(
initialRoute: isLogged ? '/home' : '/',
authService: LocalAuthService(LoginWebApi(WebAccess.LOGIN_ENDPOINT), prefs, packageInfo.version),
),
),
);
});
}
class MainPage extends StatefulWidget {
final String initialRoute;
final LocalAuthService authService;
const MainPage(
{Key? key, required this.initialRoute, required this.authService})
: super(key: key);
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
final String appName = 'My App';
int currentIndex = 0;
late final screens = [
HomeScreen(authService: widget.authService),
SecondScreen(),
ThirdScreen(),
FourthScreen()
];
}
当我 运行 应用程序时,显然是因为构造函数被调用了两次,控制台看起来像这样:
感谢阅读本文!
问题在于,当您调用 class 的构造函数时 - 每次都会创建新的唯一对象。因此 ChangeNotifierProvider 的主要 use-case 是创建一些在整个应用程序的上下文中可用的单例对象。这意味着 - 您不必为 MainPage 放置两次,而是使用 Selector 或 Consumer 访问它: ....
late final screens = [
Consumer<LoginAuthService>(builder:(context, service, _) => HomeScreen(authService: service )),
SecondScreen(),
ThirdScreen(),
FourthScreen()];
..... 您可以从 MainPage 的构造函数中删除参数