how to catch parameters using Getx in flutter

Issue

I am working on flutter route

here is my code

import 'package:flutter_application_shop/pages/home/food/popular_food.dart';
import 'package:flutter_application_shop/pages/home/main_food_page.dart';
import 'package:get/get.dart';


class RouteHelper {
  static const String popularFood = "/popular-food";


  static String getPopularFood(int pageId) => '$popularFood?pageId?=$pageId';

  static List<GetPage> routes = [
    GetPage(
        name: popularFood,
        page: () {
          var pageId = Get.parameters['pageId'];

          return PopularFoodPage(pageId: int.parse(pageId!));
        },
        transition: Transition.circularReveal),
    
  ];


}

running this code I got a correct route of /popular-food?pageId?=1 for instance but the id is not passed to the screen. I always get this error

enter image description here

I noticed that the int.parse(pageId!) returns an empty result. if I remove ! flutter will throw an error. Please I need assistance on this.

Solution

I think your problem comes from ‘$popularFood?pageId?=$pageId’; try removing the second ? and try again.

Try this

static String getPopularFood(int pageId) => '$popularFood?pageId=$pageId';

Answered By – Virginus Alajekwu – Don Solace

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *