应初始化字段“_suratList”,因为其类型 'Future<List<QuranSurat>>' 不允许为空

Field '_suratList' should be initialized because its type 'Future<List<QuranSurat>>' doesn't allow null

调试时执行出错返回空值

获取波纹管错误调试代码

lib/quran/surat_list_detail.dart:16:27: Error: Field '_ayatList' should be initialized because its type 'Future<List>' doesn't allow null.

  • 'Future' is from 'dart:async'.
  • 'List' is from 'dart:core'.
  • 'SuratAyat' is from 'package:issaq_pro/model/surat_ayat_model.dart' ('lib/model/surat_ayat_model.dart'). Future<List> _ayatList;
import 'package:flutter/material.dart';
import 'package:issaq_pro/model/surat_ayat_model.dart';

class SuratListDetail extends StatefulWidget {
  final String suratId;
  final String suratName;
  final String totalAyat;

  SuratListDetail(this.suratId, this.suratName, this.totalAyat);

  @override
  _SuratListDetailState createState() => _SuratListDetailState();
}

class _SuratListDetailState extends State<SuratListDetail> {
  Future<List<SuratAyat>> _ayatList;

  @override
  void initState() {
    super.initState();
    _ayatList = getSuratAyatData(widget.suratId);
  }

  ListView _suratListView(data) {
    return ListView.builder(
      itemCount: data.length,
      itemBuilder: (BuildContext context, index) {
        return Card(
          elevation: 8,
          child: _tile(
            data[index].ayatNumber,
            data[index].ayatText,
          ),
        );
      },
    );
  }

  ListTile _tile(
    String ayatNumber,
    String ayatText,
  ) =>
      ListTile(
        contentPadding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
        leading: Container(
          padding: EdgeInsets.only(right: 12),
          decoration: BoxDecoration(
            border: Border(
              right: BorderSide(width: 1, color: Colors.black),
            ),
          ),
          child: CircleAvatar(
            backgroundColor: Colors.amberAccent,
            child: Text(
              '$ayatNumber',
              style: TextStyle(
                color: Colors.black,
                fontSize: 12,
              ),
            ),
          ),
        ),
        title: Text(
          ayatText,
          style: TextStyle(fontSize: 24),
        ),
        trailing: Icon(
          Icons.keyboard_arrow_right,
          color: Colors.white,
          size: 30,
        ),
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.suratName,
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: Colors.black,
      ),
      body: SafeArea(
        child: Center(
          child: FutureBuilder<List<SuratAyat>>(
            future: _ayatList,
            builder: (BuildContext context,AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                List<SuratAyat> data = snapshot.data;
                return _suratListView(data);
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              }
              return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
} 

型号:

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

class QuranSurat {
  final String id;
  final String suratName;
  final String suratText;
  final String translate;
  final String totalAyat;

  QuranSurat(
      {required this.id,
      required this.suratName,
      required this.suratText,
      required this.translate,
      required this.totalAyat});

  factory QuranSurat.createSuratList(Map<String, dynamic> json) {
    return QuranSurat(
        id: json['id'].toString(),
        suratName: json['surat_name'],
        suratText: json['surat_text'],
        translate: json['surat_terjemahan'],
        totalAyat: json['count_ayat'].toString());
  }
}

Future<List<QuranSurat>> getSuratList() async {
  try {
    final apiUrl = 'https://quran.kemenag.go.id/index.php/api/v1/surat';
    final response = await http.get(Uri.parse(apiUrl));
    List result = json.decode(response.body)['data'];

    return result
        .map((surat) => new QuranSurat.createSuratList(surat))
        .toList();
  } catch (err) {
    throw err;
  }
}

只需在 Future 之前晚点添加它就可以了

late Future<List<SuratAyat>> _ayatList;