Flutter Expanded Widget returns "Null check operator used on a null value"

208 views Asked by At

I' am new to flutter. I am using ListView.builder inside a NestedScrollView to hide my top AppBar and floating action button. AppBar and floating action button hides as required. I want to use InAppWebView to display web content. However, there is a problem with the InAppWebView widget, and I can't seem to get it work. In the following code, when I run my app, I get the error "Null check operator used on a null value".

NestedScrollView(
            floatHeaderSlivers: true,
            controller: _scrollController,
            headerSliverBuilder: (context, innerBoxIsScrolled) {
              return <Widget>[
                SliverOverlapAbsorber(
                  handle:
                      NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                  sliver: SliverSafeArea(
                    sliver: SliverAppBar(
                      title: Text(widget.title),
                      floating: true,
                      snap: false,
                    ),
                  ),
                )
              ];
            },
            body: NotificationListener<UserScrollNotification>(
              onNotification: (notification) {
                final ScrollDirection direction = notification.direction;
                setState(() {
                  if (direction == ScrollDirection.reverse) {
                    _isVisible = false;
                  } else if (direction == ScrollDirection.forward) {
                    _isVisible = true;
                  }
                });
                return true;
              },
              child: ListView.builder(
                itemCount: 1,
                itemBuilder: (context, index) => Column(
                  children: [
                    Expanded(
                      child: InAppWebView(
                        initialUrlRequest: URLRequest(
                          url: Uri.https('google.com', widget.path),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),

When I remove the "Expanded" function that is wrapping my InAppWebView widget, the app just crashes. Can someone please help me be able to display my web content using the InAppWebView? All suggestions are appreciated. Thanks!

1

There are 1 answers

1
LOODAN On

There is no issue with the widget.

This error occurs when you add (!) Null check operator on Null value

final String? message;
//nullable
print(message!);
//Error: Null check operator used on a null value 

! it's called can't be null but message null.

solution

if(message != null){
    print(message);
}else{
    //return if message is null
}