Riverpd Provider
// Create the Riverpod Provider
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 ConsumerWidget{
const MyHomePage({Key? key}):super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref){
// Create instance of Provider
final name=ref.watch(nameProvider);
return Scaffold(
appBar: AppBar(
title: Text("Riverpod Provider"),
backgroundColor: Colors.orange,
),
body: SafeArea(
child: Column(
children: [
final name=ref.watch(nameProvider);
return Scaffold(
appBar: AppBar(
title: Text("Riverpod Provider"),
backgroundColor: Colors.orange,
),
body: SafeArea(
child: Column(
children: [
// Call Provider
Text("Your Provider name : " + name),
],
),
),
);
}
}
Text("Your Provider name : " + name),
],
),
),
);
}
}
No comments:
Post a Comment