【问题标题】:Data is not get loading from API in flutter. How can I fix it?数据不会在颤振中从 API 加载。我该如何解决?
【发布时间】:2021-11-09 22:55:19
【问题描述】:

我正在调用 API 数据并希望在不使用 listview.builder 的情况下显示它,但我面临的错误是数据没有被加载并且加载指示器一直处于活动状态。

例如,当我打开我的应用程序时,数据开始自行加载(因为我正在使用未来)。但它总是加载并且数据没有从 API 中获取。

我正在寻找可以帮助我解决此问题的人?

为此,这里是 myClass 代码。

class _TestState extends State<Test> {
 Future<List<dynamic>> getLiveMatches() async {
 http.Response response = await http.get(
    Uri.parse("https://api.cricket.com.au/matches/2780/50837/live")
 );
 final Map parseData = await json.decode(response.body.toString());
 var matches  = parseData['liveMatch'];
 return matches;
 }

 @override
 Widget build(BuildContext context) {
 return Scaffold(
  body:  Padding(
    padding: EdgeInsets.only(left: 10, right: 10, top: 15),
    child: RefreshIndicator(
      color: Colors.white,
      backgroundColor: Colors.purple,
      strokeWidth: 5,
      onRefresh: ()async{
        getLiveMatches();
      },
      child: Container(
        height: double.infinity,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(6),
                topRight: Radius.circular(6)
            ),
            color: Colors.white
        ),
        child: FutureBuilder<List<dynamic>>(
            future: getLiveMatches(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                List<dynamic> matches = snapshot.data;
                print(matches);
                return Column(
                  children: [
                    Text(matches.toString())
                  ],
                );
              }
              return Center(
                  child: CupertinoActivityIndicator(
                      animating: true, radius: 10));
            }
        ),
      ),
      ),
      ),
     );
     }
     }

【问题讨论】:

    标签: flutter http dart flutter-dependencies


    【解决方案1】:

    您不是在请求 json。试试看:https://api.cricket.com.au/matches/2780/50837/live?format=json

    顺便说一句,你的 onRefresh 调用什么也没做。

    【讨论】:

    • 对不起先生,它对我没有任何帮助,我会修复 onRefresh 调用
    • 它只是加载数据没有显示任何错误
    • 您可以通过检查 snapshot.error 来检查您在 FutureBuilder 中对 getLiveMatches 的调用是否包含错误而不是数据。
    猜你喜欢
    • 2020-04-09
    • 2021-12-30
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2019-06-01
    相关资源
    最近更新 更多