【问题标题】:Error on device: Null check operator used on a null value设备上的错误:空值检查运算符用于空值
【发布时间】:2021-11-25 12:09:04
【问题描述】:

我在 homepage.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;**
  1. 强调文字

** 我正在尝试使用 youtube api v3 制作视频播放器颤振应用程序,并且代码中没有错误,但是运行它后我的设备给了我错误:“null check operator used a null value”我不知道如何解决这个问题请帮助我。我是新来的。 } }

【问题讨论】:

  • 您是否在 _item!.sn-p.thumbnail 行上收到此错误?

标签: flutter dart null youtube-api


【解决方案1】:

替换您的代码:

 _channelInfo = await Services.getChannelInfo();
    _item = _channelInfo!.items[0];

用这个:

await Services.getChannelInfo().then((value) => {
 _item = value!.items[0];
});

【讨论】:

  • 现在我收到此错误“预计会找到'}'。”
猜你喜欢
  • 2021-11-04
  • 1970-01-01
  • 2022-06-19
  • 2021-09-01
  • 2022-08-06
  • 2021-12-03
  • 2021-11-17
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多