简体   繁体   中英

moving a list into a separate dart file that is used in multiple dart files?

Is it possible to move a list from a dart file into its own dart file and then import the list?

I have only gotten the four main inkwell buttons to be displayed. Other than that I can't get past the ancenstortype error. I believe the widget is not being passed on correctly.

https://pastebin.com/9zvRXpnu

 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'),
      BuyItem('Add one for \$3.49', 'assets/calculator.png'),
      BuyItem('Add one for \$0.99', 'assets/cliff bar.png'),
      BuyItem('Add one for \$1.25', 'assets/apple.png'),
      .
      .
      .
      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> foodList = [
      Product('Cliff Bar', 'assets/cliff bar.png', () {
        Navigator.of(context).push(ItemsBuyPage([buyItemList[4]]));
      }),
      Produ
      Product('Coffee', 'assets/coffee.png', () {
        Navigator.of(context).push(ItemsBuyPage([buyItemList[15]]));
      }),
    ];

    List<MainButtons> buttonList = [
      .
      .
      .
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => BrowsePage(buttonList)));
    //}
}

Once I press one of the four main buttons, I should be able to go to the screen that is navigated to.

NEW CODE

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

ERROR: The argument type 'String' can't be assigned to the parameter type 'Route'.

  MainButtons(this.image, this.name, this.onTap);

  final IconData image;
  final String name;
  final String onTap;
.
.
.
 onTap:  () {
          Navigator.of(context).push(onTap);
        },

Yes you can put the list as raw data into another file like this:

List<Product> foodList = [
  Product('Cliff Bar', 'assets/cliff bar.png')
  Product('Coffee', 'assets/coffee.png'),
];

You have to delete the Navigator there because the context in not known in that file because its not a Widget. Then in your Widget you can Wrap the list with an InkWell Widget (which allows an on press Event) and put the Navigator there.

You could also just write a whole Widget as the other File and import it. That way you have the context in that file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM