我如何在列表视图中查看从odoo api解析的json数据?

时间:2019-06-27 07:49:41

标签: api flutter odoo

我无法在列表视图中查看json。返回类型可能有问题。我要返回的列表(即“用户”)不返回任何数据,因此当我在FutureBuilder小部件中调用该函数时,我将得到null。

 OdooResponse result;
 List<Saleslist> users = [];
 Future<List<Saleslist>> _getOrders() async {
   var client = OdooClient("http://192.168.1.108:8050");
   final domain = [
     // ["sale_ok", "!=", false]
   ];
   var fields = ["id", "name"];
   // var fields = ["id", "name", "confirmation_date"];

   client
       .authenticate("admin", "admin", "flutterodoo11")
       .then((AuthenticateCallback auth) async {
     if (auth.isSuccess) {
       final user = auth.getUser();
       print("Hey ${user.name}");
     } else {
       // login fail
       print("Login failed");
     }

     client
         .searchRead("sale.order", domain, fields)
         .then((OdooResponse result) {
       if (!result.hasError()) {
         print("Succesful");
         var response = result.getResult();
         var encoded = json.encode(response['records']);
         data = encoded;
         var convertDataToJson = json.decode(encoded);
         data = convertDataToJson;
         print(data);

         for (var u in data) {
           print(u);
           Saleslist user = Saleslist(u["name"], u["id"]);
           users.add(user);
         }
       } else {
         print(result.getError());
       }
       print(users.length);
     });
   });
   return users;
 }

这是Future Builder的代码

            child: FutureBuilder(
                future: _getOrders(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.data == null) {
                    print(data);
                    return Container(
                        child: Center(
                      child: CircularProgressIndicator(),
                    ));
                  } else {
                    return ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: snapshot.data.length,
                        itemBuilder: (context, int index) {
                          return ListTile(
                            title: Text(snapshot.data.name),
                          );
                        });
                  }
                  // }
                }),
          ),

这是我从odoo_api获得的json [{id: 1, name: SO001}, {id: 2, name: SO002}, {id: 3, name: SO003}, {id: 5, name: SO005}, {id: 4, name: SO004}, {id: 7, name: SO007}, {id: 9, name: SO008}, {id: 10, name: SO009}, {id: 6, name: SO006}, {id: 8, name: Test/001}, {id: 11, name: SO010}, {id: 12, name: SO011}, {id: 13, name: SO012}, {id: 14, name: SO013}, {id: 15, name: SO014}, {id: 16, name: SO015}, {id: 17, name: SO016}, {id: 18, name: SO017}, {id: 19, name: SO018}, {id: 20, name: SO019}]

编辑:

现在,我收到“引发了另一个异常:类型'列表'不是'字符串'类型的子类型”错误...我添加了await方法,但是FutureBuilder小部件是相同的。

var client = OdooClient("http://192.168.1.34:8050");
  List<Saleslist> users = [];
  var data;
  OdooResponse result;
  Future<List<Saleslist>> _getOrders() async {
    final domain = [
      // ["sale_ok", "!=", false]
    ];
    var fields = ["id", "name"];

    // var fields = ["id", "name", "confirmation_date"];
    await client
        .authenticate("admin", "admin", "flutterodoo11")
        .then((AuthenticateCallback auth) async {
      if (auth.isSuccess) {
        final user = auth.getUser();
        print("Hey ${user.name}");
      } else {
        // login fail
        print("Login failed");
      }

      await client
          .searchRead("sale.order", domain, fields)
          .then((OdooResponse result) {
        if (!result.hasError()) {
          print("Succesful");
          var response = result.getResult();
          var encoded = json.encode(response['records']);
          // data = json.encode(encoded);
          data = json.decode(encoded);
          print(data);

          for (var u in data) {
            // print(u);
            Saleslist user = Saleslist(u["name"], u["id"]);
            users.add(user);
          }
        } else {
          print(result.getError());
        }
      });
    });
    print(users.length);
    return (users);
  }

1 个答案:

答案 0 :(得分:0)

等待等待而不是then()解决返回类型(我认为)问题。

var client = OdooClient("http://192.168.1.108:8050");
  Future<List<Saleslist>> _getOrders() async {
    // List<Saleslist> users = [];

    final domain = [];
    // var fields = ["id", "name"];
    var fields = ["id", "name", "confirmation_date", "partner_id"];
    AuthenticateCallback auth =
        await client.authenticate("admin", "admin", "flutterodoo11");
    if (auth.isSuccess) {
      final user = auth.getUser();
      print("Hey ${user.name}");
    } else {
      // login fail
      print("Login failed");
    }

    OdooResponse result = await client.searchRead("sale.order", domain, fields);
    if (!result.hasError()) {
      print("Successful");
      var response = result.getResult();
      var data = json.encode(response['records']);
      // data = json.encode(encoded);
      var decoded = json.decode(data);
      print(data);
      List<Saleslist> users = [];
      for (var u in decoded) {
        // print(u);
        Saleslist user = Saleslist(u["name"], u["id"]);
        print(user);
        users.add(user);
      }
      return users;
    } else {
      print(result.getError());
    }
  }

当期望使用“列表”类型时,snapshot.data是字符串。因此,将其更改为“ snapshot.data [index] .name”解决了整个列表视图中不显示数据的问题。

FutureBuilder(
              future: _getOrders(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                print(snapshot.data);
                if (snapshot.data == null) {
                  // print(snapshot.data);
                  return Container(
                      child: Center(child: CircularProgressIndicator()));
                } else {
                  return SizedBox(
                    // height: 500,
                    child: ListView.builder(
                        // scrollDirection: Axis.vertical,
                        itemCount: snapshot.data.length,
                        itemBuilder: (context, int index) {
                          return Card(
                            child: ListTile(
                              leading: Icon(Icons.history),
                              title: Text(
                                snapshot.data[index].name,
                                style: TextStyle(
                                  fontSize: 25,
                                  // fontFamily: "Hand_Of_Sean_Demo",
                                ),
                              ),
                              // subtitle: Text(snapshot.data[index].partner_id),
                            ),
                          );
                        }),
                  );
                }
                // }
              }),