flutter-为搜索委托搜索列表加载数据

时间:2020-08-27 17:03:58

标签: flutter

我在flutter项目中使用搜索委托。我从api获取数据,然后将它们插入一些列表中以提出搜索建议。 api网址运行正常。如您将在getProductsData()函数中看到的那样,数据已成功加载,并已添加到动态列表products中。但是,当我在加载后在构造函数中打印产品数据时,它将打印空列表。我不知道为什么会这样。这是代码示例:

class searchProducts extends SearchDelegate<String>{

  List<dynamic> products = [];

  searchProducts(){
    getProductsData();
    print("Data in the constructor");
    print(products);
  }

  Future getProductsData() async{
    var d = await http.get("http://mahmoudhelal.atwebpages.com/api/e-commerce_apis/products_api.php?fbclid=IwAR3fsifA0DO0KdhKDTke7wnVQANRy9wd9cgmmoypubXaZuQVDBNXC6Wlf5U");
    var jsonData = json.decode(d.body);
    for(int i = 0 ; i < jsonData.length; i++){
      print("before adding "+ jsonData[i]['name']);
      products.add(jsonData[i]['name']);
      print("After adding "+ products[i]);
    }

  }


  List<dynamic> history = [ 'h1', 'h2', 'h3' ];

  @override
  List<Widget> buildActions(BuildContext context) {
    return[
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: (){
          query = "nothing";
        },

      )
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      tooltip: 'Back',
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {
        //Take control back to previous page
        this.close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Text("Returned");
  }

  @override
  Widget buildSuggestions(BuildContext context) {


    List<dynamic> suggestionList = query.isEmpty? history : products.where((element) => element.startsWith(query));
    return ListView.builder( itemCount: suggestionList.length,itemBuilder: (context, index){
      return ListTile(
        leading: Icon(Icons.favorite),
        title: Text(suggestionList[index]),
      );
    });


  }
  
}

1 个答案:

答案 0 :(得分:0)

否,列表在构造函数中将始终为空。尝试将其显示在

package prelim.activities;
import java.lang.annotation.Repeatable;
import java.util.Scanner;
public class test{
    public static void main(String[] args) {
        //choosing of product
        System.out.println("Welcome to Aling Nena's Store. What do you want to buy?");
        System.out.println("\nEnter 1 for Milk");

        //scanners and booleans for product type
        Scanner scanner = new Scanner(System.in);
        String product = scanner.nextLine();
        boolean milk = true;

        //if statements for milk
        if(milk){
            System.out.println("\nThis will cost P15 each, how much would you like to buy?");

        //scanners and booleans for product price
        Scanner scanner_price = new Scanner(System.in);
        int amount = scanner_price.nextInt();
        int price = 15 * amount;
        System.out.println("\nYour product costs P" + price);

        //amount of buyers cash
        
        System.out.println("\nPlease enter how much will you pay");
        Scanner money = new Scanner(System.in);
        int cash = money.nextInt();
        
        
        
        //if statement for not enough cash
        if(cash < price){
            System.out.println("Sorry, your cash is not enough. Please enter amount again");
            // this is the part where I want to go back to "amount of buyers cash"
        }
            

        if(cash >= price){
            int change = cash - price;
            System.out.println("\nYour change is P" + change);
            System.out.println("\nThank you! Come again next time.");
            System.exit(0);
        }
        
    }
}}