Home > Mobile >  Send parameter to a class, but the class is wrapped with array
Send parameter to a class, but the class is wrapped with array

Time:09-27

So I have a method to return a page because I have 2 themes I make 2 arrays for the first and second themes. Are there any more simple ways to do it?

Because I think it's complicated if I use this method.

or is there any way to send parameters to a class, but the class is wrapped with an array?

 selectedIndex(String indexTheme, int indexPage) {
    final theme1 = [
      const HomeTest(indexTheme: 1),
      const productPage(indexTheme: 1),
      const trading(indexTheme: 1),
      const inventoryPage(indexTheme: 1),
      const masterPage(indexTheme: 1),
      const financePage(indexTheme: 1),
    ];

    final theme2 = [
      const HomeTest(indexTheme: 2),
      const productPage(indexTheme: 2),
      const trading(indexTheme: 2),
      const inventoryPage(indexTheme: 2),
      const masterPage(indexTheme: 2),
      const financePage(indexTheme: 2),
    ];

    if (indexTheme == "One") {
      return theme1[indexPage];
    } else {
      return theme2[indexPage];
    }
  }

CodePudding user response:

selectedIndex(String indexTheme, int indexPage) {
    final theme = [
      const HomeTest(indexTheme: indexTheme),
      const productPage(indexTheme: indexTheme),
      const trading(indexTheme: indexTheme),
      const inventoryPage(indexTheme: indexTheme),
      const masterPage(indexTheme: indexTheme),
      const financePage(indexTheme: indexTheme),
    ];

    return theme[indexPage];
  }

This should work, you will have to handle the fact that your indexTheme in the function is String and in the class it's an int.

  • Related