flutter:失败的断言:布尔表达式不能为null

时间:2020-02-04 01:05:57

标签: flutter

我试图运行我的项目,但此错误仍然出现

Failed assertion: boolean expression must not be null

我已经运行了30次以上,并且可以运行,但是现在不行了,我不知道出现此问题的原因,我没有在错误发生前更改代码中的任何内容,并尝试了一些解决方案而且它也不起作用,所以我现在该怎么办!

这是我的屏幕出现问题

class _TourguideDetailesState extends State<TourguideDetailes>
    with TickerProviderStateMixin {
  AppProvider appProvider;
  ScrollController scrollController = ScrollController(initialScrollOffset: 0);

  AnimationController animationController;

  @override
  void initState() {
    animationController = AnimationController(
        duration: Duration(milliseconds: 2000), vsync: this);
    animationController.forward();
    super.initState();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    appProvider = Provider.of<AppProvider>(context);

    return Scaffold(
      backgroundColor: AppTheme.getTheme().backgroundColor,
      body: Stack(
        children: <Widget>[
           NestedScrollView(
            controller: scrollController,
            physics: NeverScrollableScrollPhysics(),
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                SliverPersistentHeader(
                  pinned: true,
                  floating: true,
                  delegate: ContestTabHeader(
                    (MediaQuery.of(context).size.height),
                    appProvider.selectedTourguide,
                    () {
                      scrollController.animateTo(MediaQuery.of(context).size.height - MediaQuery.of(context).size.height / 5,
                          duration: Duration(milliseconds: 500), curve: Curves.fastOutSlowIn);
                    },
                    getGuideDetails(),
                  ),
                ),
              ];
            },
            body: ListView(
              padding: EdgeInsets.only(top: 24),
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(left: 24, right: 24),
                  child: getGuideDetails(isInList: true),
                ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Divider(
                    height: 1,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 24, right: 24),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          "Biography",
                          style: TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: 18,
                            letterSpacing: 0.5,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                    padding:
                        EdgeInsets.only(left: 24, right: 24, top: 4, bottom: 8),
                    child: Text(
                      appProvider.selectedTourguide.bio == null
                          ? ""
                          : appProvider.selectedTourguide.bio,
                      style: TextStyle(
                        fontSize: 14,
                        color: AppTheme.getTheme().disabledColor,
                      ),
                    )),
                    Padding(
                  padding: const EdgeInsets.only(left: 24, right: 24),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          "Native Language",
                          style: TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: 18,
                            letterSpacing: 0.5,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Padding(
                    padding:
                        EdgeInsets.only(left: 24, right: 24, top: 4, bottom: 8),
                    child: Text(
                      appProvider.selectedTourguide.nativeLanguage == null ? "" : appProvider.selectedTourguide.nativeLanguage,
                      style: TextStyle(
                        fontSize: 14,
                        color: AppTheme.getTheme().disabledColor,
                      ),
                    )), 
                Padding(
                  padding:
                      EdgeInsets.only(left: 24, right: 24, top: 4, bottom: 8),
                ),
                SizedBox(
                  height: 16,
                ),
                Padding(
                  padding: const EdgeInsets.only(
                      left: 16, right: 16, bottom: 16, top: 16),
                  child: Container(
                    height: 48,
                    decoration: BoxDecoration(
                      color: AppTheme.getTheme().primaryColor,
                      borderRadius: BorderRadius.all(Radius.circular(24.0)),
                      boxShadow: <BoxShadow>[
                        BoxShadow(
                          color: AppTheme.getTheme().dividerColor,
                          blurRadius: 8,
                          offset: Offset(4, 4),
                        ),
                      ],
                    ),
                    child: Material(
                      color: Colors.transparent,
                      child: InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(24.0)),
                        highlightColor: Colors.transparent,
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => BookingScreen(
                                  index: appProvider.selectedTourguide,
                                ),
                                fullscreenDialog: true,
                              ));
                        },
                        child: Center(
                          child: Text(
                            "Book now",
                            style: TextStyle(
                                fontWeight: FontWeight.w500,
                                fontSize: 16,
                                color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
                SizedBox(
                  height: MediaQuery.of(context).padding.bottom,
                ),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
            child: Container(
              height: AppBar().preferredSize.height,
              child: Row(
                children: <Widget>[
                  SizedBox(
                    height: AppBar().preferredSize.height,
                    child: Padding(
                      padding: EdgeInsets.only(top: 8, left: 8),
                      child: Container(
                        width: AppBar().preferredSize.height - 8,
                        height: AppBar().preferredSize.height - 8,
                        decoration: BoxDecoration(color: AppTheme.getTheme().disabledColor.withOpacity(0.4), shape: BoxShape.circle),
                        child: Material(
                          color: Colors.transparent,
                          child: InkWell(
                            borderRadius: BorderRadius.all(
                              Radius.circular(32.0),
                            ),
                            onTap: () {
                              if (scrollController.offset != 0.0) {
                                scrollController.animateTo(0.0, duration: Duration(milliseconds: 500), curve: Curves.easeInOutQuad);
                              } else {
                                Navigator.pop(context);
                              }
                            },
                            child: Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Icon(Icons.arrow_back, color: AppTheme.getTheme().backgroundColor),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: SizedBox(),
                  ),

                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget getGuideDetails({bool isInList = false}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        Expanded(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                appProvider.selectedTourguide.name,
                textAlign: TextAlign.left,
                style: TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 22,
                  color: isInList == null
                      ? AppTheme.getTheme().textTheme.body1.color
                      : Colors.white,
                ),
              ),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Text(
                    appProvider.selectedTourguide.address,
                    style: TextStyle(
                      fontSize: 14,
                      color: isInList == null
                          ? AppTheme.getTheme().disabledColor.withOpacity(0.5)
                          : Colors.white,
                    ),
                  ),
                  SizedBox(
                    width: 4,
                  ),
                  Icon(
                    FontAwesomeIcons.mapMarkerAlt,
                    size: 12,
                    color: AppTheme.getTheme().primaryColor,
                  ),
                ],
              ),
              isInList == null
                  ? SizedBox()
                  : Padding(
                      padding: const EdgeInsets.only(top: 4),
                      child: Row(
                        children: <Widget>[
                          Text(
                            " ${appProvider.selectedTourguide.review} Reviews",
                            style: TextStyle(
                              fontSize: 14,
                              color: isInList
                                  ? AppTheme.getTheme().disabledColor
                                  : Colors.white,
                            ),
                          ),
                        ],
                      ),
                    ),
            ],
          ),
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Row(
              children: <Widget>[
                Text(
                  appProvider.selectedTourguide.tariffValue=  null ? '' : appProvider.selectedTourguide.tariffValue,
                  textAlign: TextAlign.left,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 22,
                    color: isInList == null
                        ? AppTheme.getTheme().textTheme.body1.color
                        : Colors.white,
                  ),
                ),
                Text(
                  appProvider.selectedTourguide.currency=  null ? '' : appProvider.selectedTourguide.currency,
                  textAlign: TextAlign.left,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 22,
                    color: isInList == null
                        ? AppTheme.getTheme().textTheme.body1.color
                        : Colors.white,
                  ),
                ), 
              ],
            ), 
            Text(
              "/per hour",
              style: TextStyle(
                fontSize: 14,
                color: isInList == null
                    ? AppTheme.getTheme().disabledColor.withOpacity(0.5)
                    : Colors.white,
              ),
            ),
          ],
        ),
      ],
    );
  }
}

class ContestTabHeader extends SliverPersistentHeaderDelegate {
  final Widget ui;
  final VoidCallback callback;
  final TourGuide tourguideData;
  final double heightValue;
  ContestTabHeader(
    this.heightValue,
    this.tourguideData,
    this.callback,
    this.ui,
  );

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    var minValue = (shrinkOffset < heightValue - heightValue / 5? shrinkOffset: heightValue / 5);
    return Container(
      height: heightValue - minValue,
      decoration: BoxDecoration(
        color: AppTheme.getTheme().primaryColor,
        boxShadow: <BoxShadow>[
          BoxShadow(
            color: AppTheme.getTheme().dividerColor,
            blurRadius: 8,
            offset: Offset(4, 4),
          ),
        ],
      ),
      child: Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            top: 0,
            child: Container(
              width: MediaQuery.of(context).size.width,
              child: Image.network(
                tourguideData.photo == null
                    ? 'https://i.ytimg.com/vi/TWaReNkjTLk/maxresdefault.jpg'
                    : tourguideData.photo,
                fit: BoxFit.cover,
              ),
            ),
          ),
          Positioned(
            bottom: MediaQuery.of(context).padding.bottom + 16,
            left: 0,
            right: 0,
            child: Opacity(
              opacity: getOpValue(minValue, heightValue / 5),
              child: Column(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(left: 24, right: 24),
                    child: ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(24)),
                      child: new BackdropFilter(
                        filter:
                            new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
                        child: Container(
                          color: Colors.black12,
                          padding: const EdgeInsets.all(4.0),
                          child: Column(
                            children: <Widget>[
                              SizedBox(
                                height: 4,
                              ),
                              Padding(
                                padding: const EdgeInsets.only(
                                    left: 16, right: 16, top: 8),
                                child: ui,
                              ),
                              Padding(
                                padding: const EdgeInsets.only(
                                    left: 16, right: 16, bottom: 16, top: 16),
                                child: Container(
                                  height: 48,
                                  decoration: BoxDecoration(
                                    color: AppTheme.getTheme().primaryColor,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(24.0)),
                                    boxShadow: <BoxShadow>[
                                      BoxShadow(
                                        color: AppTheme.getTheme().dividerColor,
                                        blurRadius: 8,
                                        offset: Offset(4, 4),
                                      ),
                                    ],
                                  ),
                                  child: Material(
                                    color: Colors.transparent,
                                    child: InkWell(
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(24.0)),
                                      highlightColor: Colors.transparent,
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) =>
                                                BookingScreen(),
                                            fullscreenDialog: true,
                                          ),
                                        );
                                      },
                                      child: Center(
                                        child: Text(
                                          "Book now",
                                          style: TextStyle(
                                              fontWeight: FontWeight.w500,
                                              fontSize: 16,
                                              color: Colors.white),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 16,
                  ),
                  Center(
                    child: ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(24)),
                      child: new BackdropFilter(
                        filter:
                            new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
                        child: Container(
                          color: Colors.black12,
                          child: Material(
                            color: Colors.transparent,
                            child: InkWell(
                              highlightColor: Colors.transparent,
                              splashColor: AppTheme.getTheme()
                                  .primaryColor
                                  .withOpacity(0.2),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(38)),
                              onTap: () {
                                try {
                                  callback();
                                } catch (e) {}
                              },
                              child: Padding(
                                padding: const EdgeInsets.only(
                                    left: 16, right: 16, top: 4, bottom: 4),
                                child: Row(
                                  mainAxisSize: MainAxisSize.min,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: <Widget>[
                                    Text(
                                      'More Details',
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.w600,
                                      ),
                                    ),
                                    Padding(
                                      padding: const EdgeInsets.only(top: 2),
                                      child: Icon(
                                        Icons.keyboard_arrow_down,
                                        color: Colors.white,
                                        size: 24,
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  double getOpValue(double minValue, double maxValue) {
    var data = (1.0 - (minValue / maxValue));
    if (data < 0.0) {
      return 0.0;
    } else if (data >= 0 && data <= 1) {
      return data;
    } else {
      return 1.0;
    }
  }

  @override
  double get maxExtent => heightValue;

  @override
  double get minExtent => heightValue / 5;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}

1 个答案:

答案 0 :(得分:0)

查看“文本”小部件,希望对您有所帮助。懒得仔细阅读整个内容。顺便说一句,您可以将a == null ? 'whatever' : a替换为a ?? 'whatever'??等于if null

Row(
  children: <Widget>[
    Text(
      /// The problem is probably here, missing '='
      appProvider.selectedTourguide.tariffValue=  null ? '' : appProvider.selectedTourguide.tariffValue,
      textAlign: TextAlign.left,
      style: TextStyle(
        fontWeight: FontWeight.w600,
        fontSize: 22,
        color: isInList == null
            ? AppTheme.getTheme().textTheme.body1.color
            : Colors.white,
      ),
    ),
    Text(
      /// The problem is probably here, missing '='
      appProvider.selectedTourguide.currency=  null ? '' : appProvider.selectedTourguide.currency,
      textAlign: TextAlign.left,
      style: TextStyle(
        fontWeight: FontWeight.w600,
        fontSize: 22,
        color: isInList == null
            ? AppTheme.getTheme().textTheme.body1.color
            : Colors.white,
      ),
    ), 
  ],
),