'... flutter i get this error :type 'Null' is not a subtype of type 'Map<dynamic, dynamic>' in type cast

import 'package:flutter/material.dart';

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

@override _homeState createState() => _homeState(); }

class _homeState extends State { Map data = { } ;

@override Widget build(BuildContext context) { data = ModalRoute.of(context)!.settings.arguments as Map;

print(data);
return Scaffold(
  appBar: AppBar(
    centerTitle: true,
    title: Text(
      "Home Page" ,
      style: TextStyle(
          color: Colors.amber,
      ),
    ),
    backgroundColor: Colors.blue,

  ),
  body: Column(
    children: [
      FlatButton.icon(
          onPressed:(){
           Navigator.pushNamed(context, "/loading");
          },
        label: Text("Process"),
        icon:Icon(Icons.cloud_download),



          ),
      FlatButton.icon(
        onPressed:(){
          Navigator.pushNamed(context, "/choose_location");
        },
        label: Text("Choose Location"),
        icon:Icon(Icons.location_on),



      ),
      FlatButton.icon(
        onPressed:(){
          Navigator.pushNamed(context, "/chat");
        },
        label: Text("Chat"),
        icon:Icon(Icons.chat_outlined),



      ),
      Container(
        child: Text(""),
      )

    ],
  )



);

} }



Solution 1:[1]

I think ModalRoute.of(context)!.settings.arguments as Map the problem. You can use another way like this

var data = Map;
final _data = ModalRoute.of(context)?.settings.arguments;

if (_data is Map) {
  data = _data;
}

print(data);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quang Huy