Home > OS >  Using gesture detector, but receiving Expected a value of type 'Map<dynamic, dynamic>
Using gesture detector, but receiving Expected a value of type 'Map<dynamic, dynamic>

Time:10-16

Map? search = ModalRoute.of(context)?.settings.arguments as Map; as it will receive null value, but by using it I am getting an error. I am a beginner to flutter. Please any one help me in solving this.

         ***** From home page******
           GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(context, "/loading",arguments: {
                       "searchText": searchController.text,
                    });


          *******from loading page*********
             Map? info = ModalRoute.of(context)!.settings.arguments as Map?;

             Map? search = ModalRoute.of(context)?.settings.arguments as Map;

             city=search['searchText'];
              if(search?.isEmpty??true)
              {
                city=search['searchText'];
               }


      

CodePudding user response:

Map? search = ModalRoute.of(context)?.settings.arguments as Map;

that line should be

Map? search = ModalRoute.of(context)?.settings.arguments as Map?;

Notice the ? at the end... it could potentially be null but you're trying to cast it as non-null.

  • Related