gpt4 book ai didi

database - 如何在 Flutter 中的不同应用页面之间正确共享对象

转载 作者:行者123 更新时间:2023-12-03 04:25:56 26 4
gpt4 key购买 nike

我是 Flutter 的新手。我已经实现了一个任意类,它有一些状态,需要在 Flutter 的不同页面之间共享。我目前正在做的是我定义了一个 sqflite数据库助手和每个页面加载该对象表单数据库,在转到其他页面之前,它再次将其保存到数据库并导航到其他页面。但是,它增加了许多复杂性和错误。

另一种可能的解决方案是将其添加到 myApp()main() 中调用的类功能。但是,由于其他页面不继承 myApp() ,他们无权访问 global_object .

class MyApp extends StatelessWidget{

// I need gloabl_object accessible in different pages
//---------------------------------------------------
MyObject global_object;
//---------------------------------------------------

@override
Widget build(BuildContext context){
return MaterialApp(
title: 'App title',
debugShowCheckedModeBanner: false,
theme: new ThemeData(
// App theme
),
//home: Homepage(),
routes: { // Defining routs
// DIFFERENT PAGES THAT NEEDS TO HAVE ACCESS TO global_object
},
);
}

最佳答案

方法一 : 使用包 get_it https://pub.dev/packages/get_it
简单直接Service Locator这允许将接口(interface)与具体实现分离,并从应用程序中的任何地方访问具体实现。
示例 https://github.com/fluttercommunity/get_it/tree/master/example/lib

GetIt sl = GetIt.instance;

void main() {

sl.registerSingleton<AppModel>(AppModelImplementation());

runApp(MyApp());
}

注册后,您可以随处访问
var myAppModel = sl.get<AppModel>();

方法二 : 使用包提供者 https://pub.dev/packages/provider
完整示例 https://github.com/flutter/samples/tree/master/provider_shopper
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Using MultiProvider is convenient when providing multiple objects.
return MultiProvider(
providers: [
// In this sample app, CatalogModel never changes, so a simple Provider
// is sufficient.
Provider(create: (context) => CatalogModel()),
// CartModel is implemented as a ChangeNotifier, which calls for the use
// of ChangeNotifierProvider. Moreover, CartModel depends
// on CatalogModel, so a ProxyProvider is needed.
ChangeNotifierProxyProvider<CatalogModel, CartModel>(
create: (context) => CartModel.empty(),
update: (context, catalog, previousCart) =>
CartModel(catalog, previousCart),
),
],
child: MaterialApp(
title: 'Provider Demo',
theme: appTheme,
initialRoute: '/',
routes: {
'/': (context) => MyCatalog(),
'/cart': (context) => MyCart(),
},
),
);
}
}

访问 Provider.of<CartModel>(context);
class _CartList extends StatelessWidget {
@override
Widget build(BuildContext context) {
var itemNameStyle = Theme.of(context).textTheme.title;
var cart = Provider.of<CartModel>(context);

return ListView.builder(
itemCount: cart.items.length,

或与 Consumer<CartModel>
class _CartTotal extends StatelessWidget {
@override
Widget build(BuildContext context) {
var hugeStyle = Theme.of(context).textTheme.display4.copyWith(fontSize: 48);

return SizedBox(
height: 200,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<CartModel>(
builder: (context, cart, child) =>
Text('\$${cart.totalPrice}', style: hugeStyle)),

关于database - 如何在 Flutter 中的不同应用页面之间正确共享对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59779734/

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