flutter 检索复杂的api信息并将其放入listviewbuilder中

fnvucqvd  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(124)

我试图创建一个移动应用程序,我可以把一个名称,并从一个api获取信息后,这是工作,直到api变得更加复杂.可以分享这些信息顺便说一句.它是显示什么,但图像. api的工作时,我检索它:

{"id":"Pw_wOwAmqDmB3slQO_8PkKXLoE56qnBk5f-qdW0fKVTfvAmm",
"accountId":"_MkPpRhr6tK22xRT8TofYHPyACcmURn0cp-U9GbL1xTQzhRfvaw4ZF7P",
"puuid":"RT0wLpw9fOUMgB4HUWDVoMi6_F20W4yhut5-q1bA7Izhl3dPjv5iF2JzqXkLQJYPtf2MBMcvooMmGA",
"name":"Zeri outplay",
"profileIconId":5610,"revisionDate":1669140463310,
"summonerLevel":535}

不起作用API:

[{"leagueId":"e6a38c93-6037-4457-aa57-80a9a52be3ea",
"queueType":"RANKED_SOLO_5x5",
"tier":"PLATINUM","rank":"IV",
"summonerId":"Pw_wOwAmqDmB3slQO_8PkKXLoE56qnBk5f-qdW0fKVTfvAmm",
"summonerName":"Zeri outplay",
"leaguePoints":0,
"wins":75,
"losses":56,
"veteran":false,
"inactive":false,
"freshBlood":false,
"hotStreak":false},
{"leagueId":"a21014e6-fa8f-4f07-b533-b09a10b88bbf",
"queueType":"RANKED_FLEX_SR",
"tier":"SILVER","rank":"II",
"summonerId":"Pw_wOwAmqDmB3slQO_8PkKXLoE56qnBk5f-qdW0fKVTfvAmm",
"summonerName":"Zeri outplay",
"leaguePoints":1,
"wins":15,
"losses":5,
"veteran":false,
"inactive":false,
"freshBlood":false,
"hotStreak":true}]

下面是不起作用的api的代码:RemoteService.dart:

class SummonerRankApi {
  static Future getSummonerRank(summonerid) {
    String link = 'https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/$summonerid?api_key=$apiKey';
    return http.get(Uri.parse(link));
  }
}

首页_页面.dart

class SummonerProfileRoute extends StatefulWidget {
  String name, summonerId;
  int profileIconId;
  SummonerProfileRoute(this.name, this.summonerId, this.profileIconId);

  @override
  State<StatefulWidget> createState() {
    return _SummonerProfileRouteState(this.name, this.summonerId, this.profileIconId);
  }}

class _SummonerProfileRouteState extends State<SummonerProfileRoute> {
  List<SummonerRank> SummonerRankList = <SummonerRank>[];

  void getSummonerRankfromApi() async {
    SummonerRankApi.getSummonerRank(summonerId).then((response) {
      setState(() {
        Iterable list = json.decode(response.body);
        SummonerRankList = list.map((model) => SummonerRank.fromJson(model)).toList();
      });
    });
  }

  String name, summonerId;
  int profileIconId;
  _SummonerProfileRouteState(this.name, this.summonerId, this.profileIconId);

  @override
  void initState() {
    super.initState();
    getSummonerRankfromApi();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Route'),
      ),
      body: Center(
          child: Column(
            children: [
              Image(
                image: NetworkImage('https://ddragon.leagueoflegends.com/cdn/12.22.1/img/profileicon/$profileIconId.png'),
                height: 100,
                width: 100,
              ),
              ListView.builder(
                  itemCount: SummonerRankList.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(SummonerRankList[index].tier),
                      subtitle: Text(SummonerRankList[index].summonerName),
                    );
                  }),
            ],
          )
      ),
    );
  }
}

summoner.dart

class SummonerRank {
  String leagueId;
  String queueType;
  String tier;
  String rank;
  String summonerId;
  String summonerName;
  int leaguePoints;
  int wins;
  int losses;
  bool veteran;
  bool inactive;
  bool freshBlood;
  bool hotStreak;

  SummonerRank.fromJson(Map json)
      : leagueId = json['leagueId'],
        queueType = json['queueType'],
        tier = json['tier'],
        rank = json['rank'],
        summonerId = json['summonerId'],
        summonerName = json['summonerName'],
        leaguePoints = json['leaguePoints'],
        wins = json['wins'],
        losses = json['losses'],
        veteran = json['veteran'],
        inactive = json['inactive'],
        freshBlood = json['freshBlood'],
        hotStreak = json['hotStreak'];

  Map toJson() {
    return {'leagueId': leagueId, 'queueType': queueType, 'tier': tier, 'rank': rank,
      'summonerId': summonerId, 'summonerName': summonerName, 'leaguePoints': leaguePoints, 'wins': wins,
      'losses': losses, 'veteran': veteran, 'inactive': inactive, 'freshBlood': freshBlood, 'hotStreak': hotStreak};
  }
}

我试着用futurebuilder和gridviewbuilder构建它,但都不起作用。不过我想我知道为什么它不起作用,但我真的不能很好地解释它。它可能与不起作用的api是一个数组有关(我想)。

irlmq6kh

irlmq6kh1#

ListView.builder() Package 在Expanded()小部件中。
发布工作样品。(相同代码,不同API)

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class SummonerProfileRoute extends StatefulWidget {
  String name, summonerId;
  int profileIconId;
  SummonerProfileRoute(this.name, this.summonerId, this.profileIconId);

  @override
  State<StatefulWidget> createState() {
    return _SummonerProfileRouteState(
        this.name, this.summonerId, this.profileIconId);
  }
}

class _SummonerProfileRouteState extends State<SummonerProfileRoute> {
  List<SummonerRank> SummonerRankList = <SummonerRank>[];

  void getSummonerRankfromApi() async {
    SummonerRankApi.getSummonerRank().then((response) {
      setState(() {
        Iterable list = json.decode(response.body);
        SummonerRankList =
            list.map((model) => SummonerRank.fromJson(model)).toList();
      });
    });
  }

  String name, summonerId;
  int profileIconId;
  _SummonerProfileRouteState(this.name, this.summonerId, this.profileIconId);

  @override
  void initState() {
    super.initState();
    getSummonerRankfromApi();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Route'),
      ),
      body: Center(
          child: Column(
        children: [
          Image(
            image: NetworkImage(
                'https://ddragon.leagueoflegends.com/cdn/12.22.1/img/profileicon/$profileIconId.png'),
            height: 100,
            width: 100,
          ),
          Expanded(
            child: ListView.builder(
                itemCount: SummonerRankList.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(SummonerRankList[index].title!),
                    subtitle: Text(SummonerRankList[index].body!),
                  );
                }),
          ),
        ],
      )),
    );
  }
}

class SummonerRank {
  int? userId;
  int? id;
  String? title;
  String? body;
  SummonerRank({
    this.userId,
    this.id,
    this.title,
    this.body,
  });

  SummonerRank.fromJson(Map json)
      : userId = json['userId'],
        id = json['id'],
        title = json['title'],
        body = json['body'];

  Map toJson() {
    return {
      'userId': userId,
      'id': id,
      'title': title,
      'body': body,
    };
  }
}

class SummonerRankApi {
  static Future getSummonerRank() {
    String link = 'https://jsonplaceholder.typicode.com/posts';
    return http.get(Uri.parse(link));
  }
}

相关问题