小部件库异常提示,在null上调用了方法'> ='

时间:2019-08-20 06:26:42

标签: flutter

这是代码,可以正常工作。

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class HomeIndexPage extends StatefulWidget {
  _HomeIndexPageState createState()=>_HomeIndexPageState();
}

class _HomeIndexPageState extends State<HomeIndexPage>{
  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)
      ..init(context);
    print('设备宽度:${ScreenUtil.screenWidth}');
    print('设备高度:${ScreenUtil.screenHeight}');
    print('设备像素密度:${ScreenUtil.pixelRatio}');
    return Container(
      margin:EdgeInsets.only(top:ScreenUtil.statusBarHeight),
      child: Column(
        children: <Widget>[
          TopNavigator(),
          SwiperDiy(),
        ],
      ),
    );
  }
}

class SwiperDiy extends StatelessWidget{

  final List swiperDataList = [
    "http://images.baixingliangfan.cn/advertesPicture/20190116/20190116140738_7766.jpg",
    "http://images.baixingliangfan.cn/advertesPicture/20190116/20190116173351_2085.jpg",
    "http://images.baixingliangfan.cn/advertesPicture/20190116/20190116140753_5620.jpg",
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(333.0),
      width: ScreenUtil().setWidth(750),
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return Image.network(
            "${swiperDataList[index]}",
            fit: BoxFit.fill,
          );
        },
        itemCount: swiperDataList.length,
        pagination: new SwiperPagination(),
        autoplay: true,
      ),
    );
  }

}

class TopNavigator extends StatelessWidget {

  final List navigatorList = [
    {
      "name": "白酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170036_4477.png",
    }, {
      "name": "啤酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170044_9165.png",
    }, {
      "name": "葡萄酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170053_878.png",
    }, {
      "name": "清酒洋酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170101_6957.png",
    }, {
      "name": "保健酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170110_6581.png",
    }, {
      "name": "预调酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170124_4760.png",
    },
  ];

  Widget _gridViewItemUI(BuildContext context, item) {
    return InkWell(
      onTap: () {
        print('点击了导航');
      },
      child: Column(
        children: <Widget>[
          Image.network(
              item['image'],
              width: ScreenUtil().setWidth(85),
          ),
          Text(item['name'])
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(750),
      height: ScreenUtil().setHeight(375),
      child: GridView.count(
        crossAxisCount: 3,
        mainAxisSpacing: 30.0,
        childAspectRatio: 2/1,
        children: navigatorList.map((item) {
          return _gridViewItemUI(context, item);
        }).toList(),
      ),
    );
  }

}

here is screen shot

这是调试日志

  

I / flutter(23400):W小组件库引起的异常提示   ╞═════════════════════════════════════════════════ ══════════我/扑   (23400):引发了以下NoSuchMethodError构建   HomeIndexPage(脏,状态:I / flutter(23400):   _HomeIndexPageState#8b363):I / flutter(23400):方法'> ='在null上被调用。 I / flutter(23400):接收方:空I / flutter(23400):   尝试调用:> =(0.0)

TopNavigator类和ScreenUtil.statusBarHeight的属性似乎有问题。

当我忽略那些代码时,它工作正常。

然后我在热加载中重用那些代码,它工作正常。它是如何工作的,我不明白。

class _HomeIndexPageState extends State<HomeIndexPage>{
  @override
  Widget build(BuildContext context) {
    return Container(
//      margin:EdgeInsets.only(top:ScreenUtil.statusBarHeight),
      child: Column(
        children: <Widget>[
//          TopNavigator(),
          SwiperDiy(),
        ],
      ),
    );
  }
}

3 个答案:

答案 0 :(得分:0)

我必须标记flutter_screenutil,因为它会引起很多错误。
我只是让它起作用,您可以测试不同的高度和宽度

GridView需要收缩包装:true

完整的工作代码。

import 'package:flutter/material.dart';
//import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

void main() => runApp(MyMaterialApp());

class MyMaterialApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'flutter_typeahead demo',
      home: HomeIndexPage(),
    );
  }
}

class HomeIndexPage extends StatefulWidget {
  HomeIndexPage() : super();

  @override
  HomeIndexPageState createState()=>HomeIndexPageState();

}

class HomeIndexPageState extends State<HomeIndexPage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          //margin:EdgeInsets.only(top:ScreenUtil.statusBarHeight),
          child: Column(
            children: <Widget>[
              TopNavigator(),
              SwiperDiy(),
            ],
          ),
        ),
      ),
    );
  }
}

class SwiperDiy extends StatelessWidget{

  final List swiperDataList = [
    "http://images.baixingliangfan.cn/advertesPicture/20190116/20190116140738_7766.jpg",
    "http://images.baixingliangfan.cn/advertesPicture/20190116/20190116173351_2085.jpg",
    "http://images.baixingliangfan.cn/advertesPicture/20190116/20190116140753_5620.jpg",
  ];

  @override
  Widget build(BuildContext context) {
    /*ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)
      ..init(context);
    print('设备宽度:${ScreenUtil.screenWidth}');
    print('设备高度:${ScreenUtil.screenHeight}');
    print('设备像素密度:${ScreenUtil.pixelRatio}');*/
    return Container(
      /*height: ScreenUtil().setHeight(333.0),
      width: ScreenUtil().setWidth(750),*/
      height: 330,
      width : 500,
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return Image.network(
            "${swiperDataList[index]}",
            fit: BoxFit.fill,
          );
        },
        itemCount: swiperDataList.length,
        pagination: new SwiperPagination(),
        autoplay: true,
      ),
    );
  }

}

class TopNavigator extends StatelessWidget {

  final List navigatorList = [
    {
      "name": "白酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170036_4477.png",
    }, {
      "name": "啤酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170044_9165.png",
    }, {
      "name": "葡萄酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170053_878.png",
    }, {
      "name": "清酒洋酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170101_6957.png",
    }, {
      "name": "保健酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170110_6581.png",
    }, {
      "name": "预调酒",
      "image": "http://images.baixingliangfan.cn/firstCategoryPicture/20190131/20190131170124_4760.png",
    },
  ];

  Widget _gridViewItemUI(BuildContext context, item) {
    return InkWell(
      onTap: () {
        print('点击了导航');
      },
      child: Column(
        children: <Widget>[
          Image.network(
            item['image'],
            //width: ScreenUtil().setWidth(85),
          ),
          Text(item['name'])
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
     /*width: ScreenUtil().setWidth(750),
          height: ScreenUtil().setHeight(375),*/
     width:500,
     height:300,
      child: GridView.count(
        shrinkWrap: true,
        crossAxisCount: 3,
        mainAxisSpacing: 30.0,
        childAspectRatio: 1,
        children: navigatorList.map((item) {
          return _gridViewItemUI(context, item);
        }).toList(),
      ),
    );
  }

}

完整的演示图片

enter image description here

答案 1 :(得分:0)

使用前必须先初始化ScreenUtil。

  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
    return Container(
      margin:EdgeInsets.only(top:ScreenUtil.statusBarHeight),
      child: Column( 
        children: <Widget>[
          TopNavigator(),
          SwiperDiy(),
        ],
      ),
    );
  }

答案 2 :(得分:0)

这对我有用

pubspec.yaml

dependencies:
  flutter_screenutil: ^3.2.0

然后初始化

import 'package:flutter_screenutil/flutter_screenutil.dart';

    Widget build(BuildContext context) {
        ScreenUtil.init(context);
    .
    .
    .
    
    }