我把主函数里面的API接口函数,转移在另一个dart文件,然后在主函数导入它,方便维护代码,但是很快就发现了问题
下面是API接口函数以及测试处理
import 'dart:convert';
import 'package:http/http.dart' as http;
import './tiantianjijin/tiantianjijn_jtod.dart';
tiantianjijin(Tiantianjijin? t) async{
var headers = {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cookie': 'st_si=23513143112904; st_asi=delete; st_pvi=75677265749136; st_sp=2022-08-30%2014%3A49%3A55; st_inirUrl=https%3A%2F%2Fwww.nowcoder.com%2F; st_sn=4; st_psi=20230303151428570-112200312935-2690096899',
'Proxy-Connection': 'keep-alive',
'Referer': 'http://fund.eastmoney.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.57',
'Accept-Encoding': 'gzip',
};
http://api.fund.eastmoney.com/FundGuZhi/GetFundGZList?type=1&sort=3&orderType=desc&canbuy=1&pageIndex=1&pageSize=200&callback=jQuery1830052794852709326934_1677850528473&_=1677850531737
var url = Uri.parse('http://api.fund.eastmoney.com/FundGuZhi/GetFundGZList?type=1&sort=3&orderType=desc&canbuy=1&pageIndex=1&pageSize=200');
var res = await http.get(url, headers: headers);
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
Tiantianjijin t = Tiantianjijin.fromJson(jsonDecode(res.body));
for (var element in t!.data!.list!) { print("${element.bzdm}");}
}
这是主函数部分代码
class home extends StatefulWidget {
@override
State<home> createState() => _homeState();
}
class _homeState extends State<home> {
late String aa;
late Tiantianjijin? t=null;
@override
void initState(){
super.initState();
// xueqiu();
tiantianjijin(t);
}
结果报错,好像是说t变量没有初始化,我是打算把t变量扔给API接口函数处理并给它赋值。这个报错的原因是先后顺序不一样吗还是
LateInitializationError: Field 't' has not been initialized.