I had a loginPage dart use a list to setup the main page that uses ink well buttons. I moved the list to a different dart file and imported the list from there. But now the main page does not display any ink well buttons.

I don't remembered what I tried.

Here is the code before the changes that worked. https://pastebin.com/HMn5JUd4

Here is the non-working code and list. https://pastebin.com/H2PfKFNp https://pastebin.com/684uzGQP

 void _loginPressed() {
    // these handlers are called whenever the user tries to login, resend password or create an account
    print('The use wants to login with $_email and $_password');
    //if (_email == ""&& _password == "") {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) =>
                BrowsePage(mLists.getMainButtonsList())));
    //}
  }

void _loginPressed() { // these handlers are called whenever the user tries to login, resend password or create an account print('The use wants to login with $_email and $_password'); //if (_email == ""&& _password == "") {

List<BuyItem> buyItemList = [
  BuyItem('Add a pack of 10 for \$2.99', 'assets/scantron.png'),
  BuyItem('Add a pack of 5 for \$1.99', 'assets/pens.png'),
  BuyItem('Add one for \$1.49', 'assets/notebook.png'),
...
    Icons.local_cafe,
    'Drinks',
    () {
      Navigator.of(context).push(Page(drinksList));
    },
  ),
];
Navigator.push(
    context,
    MaterialPageRoute(
        builder: (BuildContext context) => BrowsePage(buttonList)));
//}

}

The same void function in both pastebin code is being passed buttonList but the non-working code does not display a 2 x 2 grid of buttons.


Solution 1: diegoveloper

You don't need to create a StatelessWidget to export any variable List.

Remove this empty variables:

              final List<BuyItem> buyItemList = new List();

              final List<Product> suppliesList = new List();
              final List<Product> foodList = new List();
              final List<Product> toiletriesList = new List();
              final List<Product> drinksList = new List();

              final List<MainButtons> buttonList = new List();

Remove the build method and extract those variables :

    //remove this
             @override
              Widget build(BuildContext context) {

UPDATE Your file should be something like this :

    class MyClass {
      final BuildContext context;

      MyClass(this.context);

      List<Product> suppliesList () => [
        Product('Scantrons', 'assets/scantron.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[0]]));
        }),
        Product('Pens', 'assets/pens.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[1]]));
        }),
        Product('Notebook', 'assets/notebook.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[2]]));
        }),
        Product('Calculator', 'assets/calculator.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[3]]));
        }),
      ];

       List<Product> get foodList => [
        Product('Cliff Bar', 'assets/cliff bar.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[4]]));
        }),
        Product('Apple', 'assets/apple.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[5]]));
        }),
        Product('Bannana', 'assets/bannana.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[6]]));
        }),
        Product('Gum', 'assets/gum.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[7]]));
        }),
      ];

       List<Product> get toiletriesList => [
        Product('Tissues', 'assets/tissues.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[8]]));
        }),
        Product('Hand Sanitizer', 'assets/hand sanitizer.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[9]]));
        }),
        Product('Toothbrush', 'assets/toothbrush.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[10]]));
        }),
        Product('Deoderant', 'assets/deoderant.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[11]]));
        }),
      ];

       List<Product> get drinksList => [
        Product('Energy Drink', 'assets/energy drink.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[12]]));
        }),
        Product('Water Bottle', 'assets/water bottle.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[13]]));
        }),
        Product('Gatorade', 'assets/gatorade.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[14]]));
        }),
        Product('Coffee', 'assets/coffee.png', () {
          Navigator.of(context).push(ItemsBuyPage([buyItemList[15]]));
        }),
      ];

       List<MainButtons> get buttonList => [
        MainButtons(
          Icons.domain,
          'Class Supplies',
              () {
            Navigator.of(context).push(Page(suppliesList));
          },
        ),
        MainButtons(
          Icons.local_dining,
          'Food and Snacks',
              () {
            Navigator.of(context).push(Page(foodList));
          },
        ),
        MainButtons(
          Icons.hot_tub,
          'Personal Supplies',
              () {
            Navigator.of(context).push(Page(toiletriesList));
          },
        ),
        MainButtons(
          Icons.local_cafe,
          'Drinks',
              () {
            Navigator.of(context).push(Page(drinksList));
          },
        ),
      ];
    }

      List<MainButtons> getMainButtonsList() {
        return buttonList;
      }
    }

Usage

            BrowsePage(MyClass(context).getMainButtonsList())