import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'first_provider.dart';
import 'second_provider.dart';
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => FirstProvider()),
ChangeNotifierProxyProvider<FirstProvider, SecondProvider>(
create: (_) => SecondProvider(Provider.of<FirstProvider>(_, listen: false)),
update: (_, firstProvider, secondProvider) => secondProvider..firstProvider = firstProvider,
),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Provider Example')),
body: Consumer2<FirstProvider, SecondProvider>(
builder: (context, firstProvider, secondProvider, child) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(firstProvider.data),
Text(secondProvider.combinedData),
ElevatedButton(
onPressed: () {
firstProvider.updateData('Updated Data');
},
child: Text('Update FirstProvider Data'),
),
],
);
},
),
),
);
}
}
Provider
s - medium.com/flutter-community/… – Doubleton