简体   繁体   中英

I'm trying to get a list of my nft. But output null

I am trying to get a list of my nft. But it produces null, I don't understand why. Help me please explain what is my mistake. I am trying to get a list of my nft. But it produces null, I don't understand why. Help me please explain what is my mistake. Here is an example of what I want to get:

[
{
    "id": "d3ff0163-f226-4100-ada7-1aa70903b76e",
    "title": "Bear",
    "image": "http://10.0.10.59:8000/media/85da36ddccb4a5d6656ecc99901d8109.jpg",
    "price": "50.00",
    "description": "Коллекция симпатичных пухлых медвежонков",
    "tags": "#bear, #fat",
    "instagram": "https://www.instagram.com/bear",
    "telegram": "https://www.t.me/bear",
    "facebook": "https://www.facebook.com/bear",
    "owner": 1
},

User model:

class User {
final String title;
final String image;
final String price;
final String description;
final String tags;
final String instagram;
final String telegram;
final String facebook;
final String owner;
User(
  this.title,
  this.image,
  this.price,
  this.description,
  this.tags,
  this.instagram,
  this.telegram,
  this.facebook,
  this.owner);
}

My code to get the list:

class _GetState extends State<Get> {
Future<List<User>> _getUsers() async {
Map<String, String> headers = {
  HttpHeaders.authorizationHeader:
      "Token b93b4cd7480a6b2313700a547f73f4eedd7fef01",
};
var data = await http.get("http://10.0.10.59:8000/account/api/NFT/");
data.headers.addAll(headers);
var jsonData = json.decode(data.body);
List<User> users = [];
for (var u in jsonData) {
  User user = User(
    u["title"],
    u["image"],
    u["price"],
    u["description"],
    u["tags"],
    u["instagram"],
    u["telegram"],
    u["facebook"],
    u["owner"],
  );
  users.add(user);
}
print(users.length);
return users;
}

The console displays this message:

I/flutter (18216): null
I/flutter (18216): null

You capitalized the keys in u["..."] but the original keys in the JSON are not.

Change u["Title"] to u["title"] , u["image"] , ... and it should work.

Edit: I see that your JSOn might be in an array.. so it might be u[0]["title"] but I am not sure since you didn't provide the full json response

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