在Flutter中使用ListView时出现错误“Null check operator used on a null value”

y3bcpkx1  于 2023-03-04  发布在  Flutter
关注(0)|答案(1)|浏览(352)

我正在使用一个列表视图。代码如下所示:

MediaQuery.removePadding(
    context: context,
    removeTop: true,
    child: ListView.separated(
        separatorBuilder: (context, index) {
          return Divider();
        },
        itemCount: 3,
        itemBuilder: (context, index) {
          return Padding(
            padding: EdgeInsets.all(8.0),
            child: Container(
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(right: 6),
                    child: SquareAvatar(
                        assetLocation: 'assets/images/profile_avatar.png'),
                  ),
                  Padding(
                      padding: EdgeInsets.symmetric(horizontal: 6),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            'Dr. Jose Philips',
                            style: TextStyle(
                                fontSize: AppFontSize.md,
                                color: AppColor.text,
                                fontWeight: FontWeight.bold),
                          ),
                          Text(
                            'General Physician',
                            style: TextStyle(
                                fontSize: AppFontSize.md,
                                color: AppColor.text),
                          ),
                          Text(
                            'Consulted on',
                            style: TextStyle(
                                fontSize: AppFontSize.xs,
                                color: AppColor.primaryBlue,
                                fontStyle: FontStyle.italic),
                          ),
                          Row(
                            children: <Widget>[
                              Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: <Widget>[
                                  Padding(
                                      padding: EdgeInsets.only(right: 4),
                                      child: Icon(Icons.calendar_today,
                                          color: AppColor.primaryRed)),
                                  Padding(
                                    padding:
                                        EdgeInsets.symmetric(horizontal: 4),
                                    child: Text('Aug 24, 2022'),
                                  )
                                ],
                              ),
                            ],
                          ),
                        ],
                      )),
                  Spacer(flex: 10),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.zero,
                            decoration: BoxDecoration(
                                color: AppColor.veryLightPurple,
                                borderRadius: BorderRadius.circular(30.0)),
                            child: IconButton(
                                onPressed: () => print('pressed'),
                                icon: Icon(Icons.local_hospital,
                                    color: AppColor.secondayPurple)),
                          ),
                          Container(
                            padding: EdgeInsets.zero,
                            decoration: BoxDecoration(
                                color: AppColor.veryLightBlue,
                                borderRadius: BorderRadius.circular(30.0)),
                            child: IconButton(
                                onPressed: () => print('pressed'),
                                icon: Icon(Icons.upload,
                                    color: AppColor.primaryBlue)),
                          ),
                        ],
                      ),
                      // TextButton(
                      //   style: ButtonStyle(
                      //     foregroundColor:
                      //         MaterialStateProperty.all<
                      //             Color>(Colors.blue),
                      //   ),
                      //   onPressed: () {},
                      //   child: Text('TextButton'),
                      // )
                      SolidButton(
                          onTap: () {},
                          text: 'text',
                          color: AppColor.lightBlue)
                    ],
                  ),
                ],
              ),
            ),
          );
        }))

我做了一个自定义的小部件SolidButton(),代码如下:

import 'package:flutter/material.dart';
import 'package:nextgen_myhealth_patients/utility/styles/sizes.dart';

class SolidButton extends StatelessWidget {
  const SolidButton(
      {super.key,
      required this.onTap,
      this.color=Colors.white,
      required this.text});

  final VoidCallback onTap;
  final Color color;
  final String text;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: double.infinity,
        decoration:
            BoxDecoration(borderRadius: BorderRadius.circular(5), color: color),
        padding: const EdgeInsets.all(10),
        child: Center(
          child: Text(
            text,
            style: const TextStyle(color: Colors.white, fontSize: AppFontSize.md),
          ),
        ),
      ),
    );
  }
}

每当我在ListView Null check operator used on a null value The relevant error-causing widget was ListView中使用这个自定义小部件时,这个错误就会发生。但是如果我使用内置的flutter小部件,比如TextButton(),listview就可以正常工作。
我在找问题。有什么问题吗?

monwx1rj

monwx1rj1#

内部使用width: double.infinity时出现问题。您可以从SolidButton中删除它

return GestureDetector(
      onTap: onTap,
      child: Container(
        // width: double.infinity,

论Flutter
约束向下。大小向上。父对象设置位置。有关constraints的详细信息

相关问题