- Welcome to Geeksww.com
Pass an object through navigator.pushnamed using provider
Here is an example of how you can pass an object through a route using the Navigator.pushNamed()
method and Provider in Flutter:
import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class MyObject { final int id; final String name; MyObject(this.id, this.name); } class MyObjectProvider extends ChangeNotifier { MyObject _object; MyObject get object => _object; set object(MyObject value) { _object = value; notifyListeners(); } } void main() { runApp( ChangeNotifierProvider( create: (context) => MyObjectProvider(), child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( routes: { '/detail': (context) => DetailPage(), }, home: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: RaisedButton( onPressed: () { final object = MyObject(1, 'Object'); Provider.of<MyObjectProvider>(context, listen: false).object = object; Navigator.of(context).pushNamed('/detail'); }, child: Text('Go to detail page'), ), ), ); } } class DetailPage extends StatelessWidget { @override Widget build(BuildContext context) { final object = Provider.of<MyObjectProvider>(context).object; return Scaffold( body: Center( child: Text('Object ID: ${object.id}'), ), ); } }
In this example, the MyObject
class represents an object that you want to pass through a route. The MyObjectProvider
class is a ChangeNotifier that holds the MyObject object and exposes it to other widgets through a getter method.
The HomePage
widget has a button that, when clicked, sets the MyObject
object in the MyObjectProvider
using the object setter method
Did this tutorial help a little? How about buy me a cup of coffee?
Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.
IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.
tags cloud
popular searches
free download for mysql database server 5.1.5, bison, gearman, source code, php, install cairo, laptop, mysql, java, linux, install mysql, mysql initialization, mysql mysql, tools, ubuntu
Similar Tutorials:
- How to create a constructor that clones another object in Dart
- Options to store data locally in a flutter app
- Dart - x positional argument(s) expected, but 0 found.
- How to create and maintain a fixed length queue in dart?
- ?? - the null-coalescing operator in Dart
Tutorials in 'Web Development > Dart' (more):
- How to create and maintain a fixed length queue in dart?
- Options to store data locally in a flutter app
- Dart - x positional argument(s) expected, but 0 found.
- ?? - the null-coalescing operator in Dart
- How to create a constructor that clones another object in Dart
Comments (write a comment):
0 comments so far. Be the first one to leave a comment on this article.
leave a comment