Read Data with Consumer in Riverpod
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// Create Provider
final nameProvider = Provider<String>((ref){
return 'Hello Avi';
});
void main(){
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Riverpod',
debugShowCheckedModeBanner: false,
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget{
const MyHomePage({Key? key}):super(key: key);
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text("Riverpod Provider"),
backgroundColor: Colors.orange,
),
body: SafeArea(
child: Column(
children: [
Text("Your Provider name : "),
// Create Consumer
Consumer(builder: (context,ref,child){
final name=ref.watch(nameProvider);
return Text(name);
}),
],
),
),
);
}
}
Consumer(builder: (context,ref,child){
final name=ref.watch(nameProvider);
return Text(name);
}),
],
),
),
);
}
}
No comments:
Post a Comment