skip to Main Content

import ‘package:flutter/material.dart’;

class Home extends StatefulWidget {
const Home({ Key? key }) : super(key: key);

@override
State createState() => _HomeState();
}

class _HomeState extends State {
Map? data = {};

@override
void initState() {

super.initState();

}

@override
Widget build(BuildContext context) {

final Map data =  ModalRoute.of(context)!.settings.arguments as Map;
print(data);





return Scaffold(
  body: SafeArea(
    child: Padding(
      padding: const EdgeInsets.fromLTRB(0, 120, 0, 0),
      child: Column(
        children: [
          TextButton.icon(
            onPressed: () {
              Navigator.pushNamed(context,'/location');
            }, 
            icon: const Icon(Icons.edit_location),  
            label: const Text('Edit Location'),
            ),
            const SizedBox(height: 20,),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  data['location'],
                  style: const TextStyle(
                    fontSize: 20.0,
                    letterSpacing: 2,

                  ),
                )
              ],
              ),
            const SizedBox(height: 20,),
            Text(
              data['time'],
              style: const TextStyle(
                fontSize: 66,
              ),
            ),
        ],
      ),
    ),
    ),
);

}
}

{location: Berlin, flag: germany.jpg, time: 9:08 AM}

2

Answers


  1. try with

    final Map? data =  ModalRoute.of(context)!.settings.arguments as Map;
    
    

    By this Null safety operator you are telling the returned value could be null as well. Once the value is received handle the null value as per your requirement.

    Login or Signup to reply.
  2. you have to replace Map with a custom class,

    final Map? data =  ModalRoute.of(context)!.settings.arguments as Map;
    

    class ScreenArguments {
      final String title;
      final String message;
    
      ScreenArguments(this.title, this.message);
    }
    
    final ScreenArguments? data =  ModalRoute.of(context)!.settings.arguments as ScreenArguments;
    

    you have to pass arguments at pushNamed function

                TextButton.icon(
                  onPressed: () {
                    Navigator.pushNamed(context,'/location', arguments: ScreenArguments("halo", "Message"));
                  },
                  icon: const Icon(Icons.edit_location),
                  label: const Text('Edit Location'),
                ),
    

    you have also to place appBar:AppBar() in Scaffold for back Button
    it can be empty

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search