gpt4 book ai didi

整个 Flutter 应用程序的 Redux 状态不工作

转载 作者:IT王子 更新时间:2023-10-29 06:46:22 25 4
gpt4 key购买 nike

我会先说我是 Dart 和 Flutter 的新手,所以我的问题可能很明显,但到目前为止对我来说是虚幻的。

我正在努力将 Redux 集成到我的 Flutter 应用程序中。在启动后的第一个 View 中管理状态按预期工作。但是,在应用程序启动后通过路由名称移动到新的“ View ”时使用。

onTap: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/dashboard',
(Route<dynamic> route) => false
);
},

仪表板小部件如下所示:

class DashboardHome extends StatelessWidget {

@override
Widget build(BuildContext context) {

final _l10n = HnLocalizations.of(context);

return new Scaffold(
appBar: HnAppBar.dashboardSettings(),
bottomNavigationBar: new HnTabBar(),
backgroundColor: HnColors.purple,
body: new StoreConnector(
converter: (store) => store.state.activeSegment,
builder: (context, int) => new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage(AppGraphics.bgWorldly),
fit: BoxFit.fitWidth,
alignment: new Alignment(0.0, 0.0),
),
),

View 抛出错误

NoSuchMethodError: The getter 'store' was called on null
Receiver: null
Tried calling: store

我的 Main 看起来像这样:

void main() {

SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp
]);

final store = new Store(
appReducer,
initialState: new HnAppState(),
);


runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: [
const HnLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', 'US'),
],
onGenerateTitle: (BuildContext context) =>
HnLocalizations.of(context).appName,
color: HnColors.canvas,
theme: HnTheme.baseTheme(),
home: new StoreProvider(
store: store,
child:new AppLaunch()
),
routes: <String, WidgetBuilder>{
"/home": (BuildContext context) => new WelcomeHome(),
"/dashboard": (BuildContext context) => new DashboardHome(),
"/dashboard/settings/account": (BuildContext context) => new AccountProfileSegmentHome()
},
));

}

根据我对文档的理解,一旦在应用程序顶部设置了 StoreProvider,“StoreConnector”就应该可以访问 store。对我来说,情况似乎并非如此。我的第一个倾向是通过路由传递 store,如下所示:

"/dashboard": (BuildContext context) => new DashboardHome(store)

但这似乎非常不合时宜,并且违背了从应用程序顶部访问状态的目的。

我错过了什么?

最佳答案

你使用什么版本的 flutter_redux?新版本的 flutter_redux 添加了对 dart 2 的支持,因此您可能必须遵循 https://pub.dartlang.org/packages/flutter_redux#-readme-tab- 中的“Dart 2 迁移指南”

你试过这样做吗:

runApp(new StoreProvider(
store: store,
child:new MaterialApp(...)

当您推送不同的路由时,将 StoreProvider 放在您的主页中将会成为一个问题。Brian 已经使用 StoreProvider 位于小部件树顶部的示例更新了自述文件 ( https://github.com/brianegan/flutter_redux)。

关于整个 Flutter 应用程序的 Redux 状态不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49440669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com