主页.dart 中的我的代码
// ignore_for_file: sized_box_for_whitespace, prefer_const_constructors, unused_element, avoid_unnecessary_containers
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:friendlycourse/UI/modals/channel_info.dart';
import 'package:friendlycourse/UI/net/services.dart';
import 'package:friendlycourse/UI/screens/drawer.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// get searchBar => null;
ChannelInfo? _channelInfo;
Item? _item;
// late bool _loading;
@override
void initState() {
super.initState();
// _loading = true;
_getChannelInfo();
}
_getChannelInfo() async {
_channelInfo = await Services.getChannelInfo();
_item = _channelInfo!.items[0];
// setState(() {
// _loading = false;
// });
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: MyDrawer(),
appBar: AppBar(
title: Text(widget.title),
// actions: <Widget>[
// IconButton(icon: Icon(Icons.search),onPressed: (){},)
// ],
),
body: Container(
child: Column(
children: [
_buildInfoView(),
],
),
),
);
}
_buildInfoView() {
return Container(
child: Card(
child: Row(
children: [
CircleAvatar(
backgroundImage: CachedNetworkImageProvider(
_item!.snippet.thumbnails.medium.url,
),
),
SizedBox(
width: 20,
),
Text(
_item!.snippet.title,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
),
)
],
),
));
}
}
My code in services.dart
import 'dart:io';
import 'package:friendlycourse/UI/modals/channel_info.dart';
import 'package:friendlycourse/UI/net/constants.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
class Services {
static const channelId = 'UCBwmMxybNva6P_5VmxjzwqA';
static const _baseUrl = 'youtube.googleapis.com';
static Future<ChannelInfo> getChannelInfo() async {
Map<String, String> parameters = {
'part': 'snippet',
'id': channelId,
'key': Constant.apikey,
};
Map<String, String> headers = {
HttpHeaders.contentTypeHeader: 'application/json'
};
Uri uri = Uri.https(_baseUrl, 'youtube/v3/search', parameters);
Response response = await http.get(uri, headers: headers);
print(response.body);
ChannelInfo channelInfo = channelInfoFromJson(response.body);
return channelInfo;**
- 强调文本
**我正试图使用youtube api v3制作视频播放器flutter应用程序,代码中没有错误,但在运行后,我的设备给了我错误:";空校验运算符使用了一个空值";我不知道如何解决这个问题,请帮帮我。我是个新手。}}
补充您的代码:
_channelInfo = await Services.getChannelInfo();
_item = _channelInfo!.items[0];
这个:
await Services.getChannelInfo().then((value) => {
_item = value!.items[0];
});