Null Check Operator Used On A Null Value - StaggeredGridView
I'm working on a 'Notes' app.
So far, it has a HomeScreen()
and Note()
widget. The first is the screen where the notes are to be displayed and the second is a custom widget which is inflated by the way the notes are going to be displayed. I'm using Card()
to display each note.
To dynamically set the sizes of the Cards, I'm using StaggeredGridView
.
Here's my code.. notesList
is the array of notes.
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get_it/get_it.dart';
import '../models/notes_list.dart';
import '../services/notes_service.dart';
import '../note.dart';
import './add_note_screen.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
NotesService get service => GetIt.I<NotesService>();
List<NotesList> notesList = [];
@override
void initState() {
notesList = service.getNotes();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: notesList.length == 0
? AppBar(title: Text('Create a Note'))
: AppBar(title: Text('Safe')),
body: StaggeredGridView.builder(
padding: EdgeInsets.all(10),
itemCount: notesList.length,
gridDelegate: SliverStaggeredGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
staggeredTileBuilder: (index) => StaggeredTile.fit(1),
),
itemBuilder: (_, index) => Note(
notesList[index].noteTitle,
notesList[index].noteContent,
notesList[index].noteDateTime,
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => Navigator.push(
context, MaterialPageRoute(builder: (_) => AddNoteScreen())),
),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
}
}
I want to display only 2 notes across the screen and with the help of above code, I'm able to achieve that.
One problem I was already facing was that the StaggeredGridView
was scrolling infinitely. My actual work today was to solve this issue. However, I stumbled upon a new issue and that's
Null check operator used on a null value
My debug console points to the StaggeredGridView as the source of this error. I'm trying to find a solution, only I couldn't.
Here's the error stack trace.
The following assertion was thrown during performLayout():
'package:flutter_staggered_grid_view/src/rendering/sliver_variable_size_box_adaptor.dart': Failed assertion: line 365 pos 12: 'child != null': is not true.
The relevant error-causing widget was
SliverStaggeredGrid
..\…\widgets\staggered_grid_view.dart:506
When the exception was thrown, this was the stack
#2 RenderSliverVariableSizeBoxAdaptor.paintExtentOf
package:flutter_staggered_grid_view/…/rendering/sliver_variable_size_box_adaptor.dart:365
#3 RenderSliverStaggeredGrid.performLayout
package:flutter_staggered_grid_view/…/rendering/sliver_staggered_grid.dart:317
#4 RenderObject.layout
package:flutter/…/rendering/object.dart:1777
#5 RenderSliverEdgeInsetsPadding.performLayout
package:flutter/…/rendering/sliver_padding.dart:132
#6 RenderSliverPadding.performLayout
package:flutter/…/rendering/sliver_padding.dart:371
Answer
I've discovered StaggeredGridView.builder
throws that error when it's not enough items to cover the screen, if you use shrinkWrap: true
you'll have to provide bounded height, which is not a universal solution,
but according to your expectations, StaggeredGridView.countBuilder
is more appropriate to use in your situation.
So, I've used StaggeredGridView.countBuilder
, it requires all necessary parameters and it works for me with your HomeScreen
code:
StaggeredGridView.countBuilder(
padding: EdgeInsets.all(10),
itemCount: notesList.length,
crossAxisCount: 2,
staggeredTileBuilder: (index) => StaggeredTile.fit(1),
itemBuilder: (_, index) => Note(
notesList[index].noteTitle,
notesList[index].noteContent,
notesList[index].noteDateTime,
),
)
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?