Flutter - 在多个提供者之间共享数据
Flutter - Sharing data between muliple Providers
我正在构建一个 Flutter 应用程序,它有一个包含会话 ID 和许多其他“全局”属性的主提供程序。
class AppService with ChangeNotifier {
String _sessionId;
String _name;
...
}
我还有其他几个提供程序可以处理非常具体的事情(AppService 也开始变大,所以不得不将其拆分)。
我的问题是如何从其他服务(例如将它们命名为 ServiceB、ServiceC 和 ServiceD)访问 AppService 中的 _sessionId、名称和其他属性?也可以从另一个如 ServiceB 调用 AppService 中的函数吗?
class ServiceB with ChangeNotifier {
...
}
class ServiceC with ChangeNotifier {
...
}
class ServiceD with ChangeNotifier {
...
}
我看到有人提到 ChangeNotifierProxyProvider,但不确定这是否能满足我的需求,因为我需要在多个其他 类.
中访问 AppService
这是我的提供商当前的设置方式..
MultiProvider(
providers: [
ChangeNotifierProvider<AppService>(
create: (_) => AppService(),
),
ChangeNotifierProvider<ServiceB>(
create: (_) => ServiceB(),
),
ChangeNotifierProvider<ServiceC>(
create: (_) => ServiceC(),
),
ChangeNotifierProvider<ServiceD>(
create: (_) => ServiceD(),
),
],
child: const InitSysApp(),
)
您可以使用 provider
发布的 ProxyProvider
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class Person with ChangeNotifier {
Person({this.name, this.age});
final String name;
int age;
void increaseAge() {
this.age++;
notifyListeners();
}
}
class Job with ChangeNotifier {
Job(
this.person, {
this.career,
});
final Person person;
String career;
String get title {
if (person.age >= 28) return 'Dr. ${person.name}, $career PhD';
return '${person.name}, Student';
}
}
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<Person>(
create: (_) => Person(name: 'Yohan', age: 25)),
ChangeNotifierProxyProvider<Person, Job>(
create: (BuildContext context) =>
Job(Provider.of<Person>(context, listen: false)),
update: (BuildContext context, Person person, Job job) =>
Job(person, career: 'Vet'),
),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Provider Class'),
),
body: Align(
alignment: Alignment.center,
child: Container(
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Hi, may name is ${context.select((Job j) => j.person.name)}',
style: Theme.of(context).textTheme.headline6,
),
Text('Age: ${context.select((Job j) => j.person.age)}'),
Text(context.watch<Job>().title),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () =>
Provider.of<Person>(context, listen: false).increaseAge(),
),
);
}
}
我正在构建一个 Flutter 应用程序,它有一个包含会话 ID 和许多其他“全局”属性的主提供程序。
class AppService with ChangeNotifier {
String _sessionId;
String _name;
...
}
我还有其他几个提供程序可以处理非常具体的事情(AppService 也开始变大,所以不得不将其拆分)。
我的问题是如何从其他服务(例如将它们命名为 ServiceB、ServiceC 和 ServiceD)访问 AppService 中的 _sessionId、名称和其他属性?也可以从另一个如 ServiceB 调用 AppService 中的函数吗?
class ServiceB with ChangeNotifier {
...
}
class ServiceC with ChangeNotifier {
...
}
class ServiceD with ChangeNotifier {
...
}
我看到有人提到 ChangeNotifierProxyProvider,但不确定这是否能满足我的需求,因为我需要在多个其他 类.
中访问 AppService这是我的提供商当前的设置方式..
MultiProvider(
providers: [
ChangeNotifierProvider<AppService>(
create: (_) => AppService(),
),
ChangeNotifierProvider<ServiceB>(
create: (_) => ServiceB(),
),
ChangeNotifierProvider<ServiceC>(
create: (_) => ServiceC(),
),
ChangeNotifierProvider<ServiceD>(
create: (_) => ServiceD(),
),
],
child: const InitSysApp(),
)
您可以使用 provider
发布的 ProxyProvider
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class Person with ChangeNotifier {
Person({this.name, this.age});
final String name;
int age;
void increaseAge() {
this.age++;
notifyListeners();
}
}
class Job with ChangeNotifier {
Job(
this.person, {
this.career,
});
final Person person;
String career;
String get title {
if (person.age >= 28) return 'Dr. ${person.name}, $career PhD';
return '${person.name}, Student';
}
}
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<Person>(
create: (_) => Person(name: 'Yohan', age: 25)),
ChangeNotifierProxyProvider<Person, Job>(
create: (BuildContext context) =>
Job(Provider.of<Person>(context, listen: false)),
update: (BuildContext context, Person person, Job job) =>
Job(person, career: 'Vet'),
),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Provider Class'),
),
body: Align(
alignment: Alignment.center,
child: Container(
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(5.0),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Hi, may name is ${context.select((Job j) => j.person.name)}',
style: Theme.of(context).textTheme.headline6,
),
Text('Age: ${context.select((Job j) => j.person.age)}'),
Text(context.watch<Job>().title),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () =>
Provider.of<Person>(context, listen: false).increaseAge(),
),
);
}
}