升级颤振后颤振标签栏错误“getter'key'被称为null”

Posted

技术标签:

【中文标题】升级颤振后颤振标签栏错误“getter\'key\'被称为null”【英文标题】:Flutter tab bar error after upgraded flutter "The getter 'key' was called on null"升级颤振后颤振标签栏错误“getter'key'被称为null” 【发布时间】:2020-02-28 19:09:20 【问题描述】:

以下 NoSuchMethodError 被抛出构建 MediaQuery(MediaQueryData(size: Size(360.0, 640.0), devicePixelRatio: 3.0,textScaleFactor:1.0,platformBrightness:Brightness.light,填充:EdgeInsets.zero,viewPadding:EdgeInsets.zero,viewInsets: EdgeInsets.zero,物理深度:1.7976931348623157e+308, alwaysUse24HourFormat:假,accessibleNavigation:假, disableAnimations: false, invertColors: false, boldText: false)): 在 null 上调用了 getter 'key'。接收者:null 尝试调用:key

用户创建的导致错误的小部件的祖先是:Scaffold file:///home/midhilajm/Downloads/al-qasier-flutter-solving_issue_20/lib/orderDetails/OrderDetails.dart:27:16 抛出异常时,这是堆栈:

0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)

1 个新的 KeyedSubtree.wrap(包:flutter/src/widgets/basic.dart:6605:27)

2 KeyedSubtree.ensureUniqueKeysForList(包:flutter/src/widgets/basic.dart:6623:44)

3 _TabBarViewState._updateChildren(包:flutter/src/material/tabs.dart:1237:37)

4 _TabBarViewState.initState (package:flutter/src/material/tabs.dart:1206:5) ...

════════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══

导入'包:alqaiser/cart/MyCart.dart'; 导入“包:alqaiser/orderHistory/OrderHistoryModule.dart”; 导入“包:alqaiser/orderHistory/OrderItem.dart”; 导入'包:alqaiser/toolbar/MyToolBar.dart'; 导入“包:alqaiser/utlity/Milla.dart”; 导入'package:flutter/material.dart';

class OrderDetails extends StatefulWidget 
  OrderItem order;
  OrderDetails(this.order) 
  @override
  _OrderDetailsState createState() => _OrderDetailsState(order: order);


class _OrderDetailsState extends State<OrderDetails> 
  OrderItem order;
  _OrderDetailsState(this.order) 

  

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: MyToolBar().getAppBar(context),
      body: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: TabBar(
              tabs: [
                Tab(
                  text: "Work followup",
                ),
                Tab(
                  text: "Payment ",
                ),
              ],
            ),
          ),
          body: TabBarView(children: [getWorkStatus(), getPaymentStatus()]),
        ),
      ),
    );
  

  Widget getPaymentStatus() 
    if (this.order.paymentS != null)
      return ListView.builder(
          itemCount: this.order.paymentS.length,
          itemBuilder: (BuildContext context, int index) 
            return Bubble(message: this.order.paymentS[index]);
          );
  

  Widget getWorkStatus() 
    if (this.order.statues != null)
      return ListView.builder(
          itemCount: this.order.statues.length,
          itemBuilder: (BuildContext context, int index) 
            return Bubble(message: this.order.statues[index]);
          );
  


class Bubble extends StatelessWidget 
  Color bg =Colors.blueGrey ;
  String message1="",message2="";
  Bubble(
      this.message,
      this.time = "12",
      this.delivered = false,
      this.isMe = false);

  final String message, time;
  final delivered, isMe;
  List<String> messageSplit = [];
  @override
  Widget build(BuildContext context) 
    messageSplit = message.split(":");
    if(messageSplit.length>=1?messageSplit[0]!=null?true:false:false)
  message1=messageSplit[0];
    if(messageSplit.length>=3?
  messageSplit[2]!=null?true:false:false)
    message2=messageSplit[2];
    if(messageSplit.length>=2?
    messageSplit[1]!=null?true:false:false)
    bg=isMe ? Colors.white :HexColor(messageSplit[1]);
    final align = isMe ? CrossAxisAlignment.start : CrossAxisAlignment.end;
    final icon = delivered ? Icons.done_all : Icons.done;
    final radius = isMe
        ? BorderRadius.only(
            topRight: Radius.circular(5.0),
            bottomLeft: Radius.circular(10.0),
            bottomRight: Radius.circular(5.0),
          )
        : BorderRadius.only(
            topLeft: Radius.circular(5.0),
            bottomLeft: Radius.circular(5.0),
            bottomRight: Radius.circular(10.0),
          );
    return Column(
      crossAxisAlignment: align,
      children: <Widget>[
        Container(
          margin: const EdgeInsets.all(3.0),
          padding: const EdgeInsets.all(8.0),
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                  blurRadius: .5,
                  spreadRadius: 1.0,
                  color: Colors.black.withOpacity(.12))
            ],
            color: bg,
            borderRadius: radius,
          ),
          child:Column(
            children: <Widget>[
              Container(

              child:    Text(message1,textAlign: TextAlign.right,style: TextStyle(color: Colors.white),),
          )
          ,
              Text(message2,textAlign: TextAlign.right,
                  style: TextStyle(
                    color: Colors.white70,
                    fontSize: 10.0,
                  )),
            ],
          ),
        )
      ],
    );
  

【问题讨论】:

请分享完整的可运行代码,以便我们为您提供帮助。 【参考方案1】:

你不能有可能不返回任何值的方法。当 payment == null 时会返回什么?

Widget getPaymentStatus() 
if (this.order.paymentS != null)
  return ListView.builder(
      itemCount: this.order.paymentS.length,
      itemBuilder: (BuildContext context, int index) 
        return Bubble(message: this.order.paymentS[index]);
      );

您应该处理所有条件,例如:

Widget getPaymentStatus() 
 if (this.order.paymentS != null)
   return ListView.builder(
      itemCount: this.order.paymentS.length,
      itemBuilder: (BuildContext context, int index) 
        return Bubble(message: this.order.paymentS[index]);
      );
 
 else
  return Container()
 

【讨论】:

以上是关于升级颤振后颤振标签栏错误“getter'key'被称为null”的主要内容,如果未能解决你的问题,请参考以下文章

升级到最新版本后出现颤振错误

升级我的颤振后如何修复以下错误

升级颤振后命令不起作用

将颤振从 2.5 升级到 2.8 后出错

我的颤振应用程序的标签栏视图不起作用

错误:无法“发布升级”颤振工具。 5 秒后重试...(还剩 9 次尝试)