扑 "bad state: stream has already been listened to" API 不是 运行

Flutter "bad state: stream has already been listened to" API not running

我正在尝试 运行 一个 运行 来自“api-football” (https://www.api-football.com) 的 API 的应用程序并获取错误“错误状态:流已被收听”。

我正在使用 class FutureBuilder 和 htpp 包。

我的main.dart:


// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';
import 'package:placar_brasileirao/api_manager.dart';
import 'package:placar_brasileirao/page_body.dart';
import 'package:placar_brasileirao/soccermodel.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
  return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(
            'BRASILEIRÃO 2022',
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Colors.transparent,
          elevation: 0,
        ),
        body: FutureBuilder<SoccerMatch>(
            future: SoccerApi().fetchSoccerMatch(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                print((snapshot.data).length);
                return PageBody(snapshot.data);
              } else if (snapshot.hasError) {
                return Center(
                  child: Text('${snapshot.error}'),
                );
              }
              return Center(
                child: CircularProgressIndicator(),
              );
            }),
      ),
    );
  }
}

我管理的文件 API:

import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:placar_brasileirao/soccermodel.dart';



class SoccerApi {

  Future<SoccerMatch> fetchSoccerMatch() async {
    var headers = {
      'x-rapidapi-host': 'v3.football.api-sports.io',
      'x-rapidapi-key': 'c15180946e4982c30d9b63bc534d5f3c'
    };

    var request = http.Request('GET', Uri.parse('https://v3.football.api-sports.io/fixtures/rounds?season=2021&league=39'));

    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();


    if (response.statusCode == 200) {
      print(await response.stream.bytesToString());
      return SoccerMatch.fromJson(jsonDecode(await response.stream.bytesToString()));
    }
    else {
      print(response.reasonPhrase);
      throw Exception('Failed to load album');
    }
  }
}

代码是 运行ning 但它没有返回我的“PageBody”

https://i.stack.imgur.com/SAvK5.png

发生这种情况是因为您正在收听 response.stream 两次。尝试听一次并将结果存储在一个变量中。

if (response.statusCode == 200) {
      final responseData = await response.stream.bytesToString();
      print(responseData);
      return SoccerMatch.fromJson(jsonDecode(responseData));
}