输入相同列表,但输出为空白

时间:2019-04-19 00:30:27

标签: dart flutter

我让loginPage dart使用列表来设置使用墨水孔按钮的主页。我将列表移到另一个Dart文件,然后从那里导入了列表。但是现在主页上没有显示任何墨水井按钮。

我不记得我尝试过什么。

这是更改生效之前的代码。 https://pastebin.com/HMn5JUd4

这是无效代码和列表。 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(){     //只要用户尝试登录,重新发送密码或创建帐户,就会调用这些处理程序     print('用户希望使用$ _email和$ _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.

1 个答案:

答案 0 :(得分:0)

您无需创建StatelessWidget即可导出任何变量列表。

删除此空变量:

              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();

删除build方法并提取这些变量:

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

更新 您的文件应该是这样的:

    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;
      }
    }

用法

            BrowsePage(MyClass(context).getMainButtonsList())