'Error is "The getter 'length' was called on null. Receiver: null Tried calling: length"
I am trying to develop a flutter app. This app is like a dating app. After writing all the code, everything is working fine as I expected But on the home screen, I got an exception, where I know where the exception and what it means, but i do not have any clue how i could solve the problem. I hope some of you guys could help me.
This is the code where the error is thrown:
import 'package:buis_talk/blocs/swipe/swipe_bloc.dart';
import 'package:buis_talk/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:buis_talk/models/models.dart';
class HomeScreen extends StatelessWidget {
static const String routeName = '/home';
// const HomeScreen({Key? key}) : super(key: key);
static Route route() {
return MaterialPageRoute(
settings: const RouteSettings(name: routeName),
builder: (context) => HomeScreen(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(
title: 'Home',
),
body: BlocBuilder<SwipeBloc, SwipeState>(
builder: (context, state) {
var userCount = state.users.length;
return Column(
children: [
InkWell(
onDoubleTap: () {
Navigator.pushNamed(context, '/users',
arguments: state.users[0]
);
},
child: Draggable<User>(
data: state.users[0],
child: UserCard(user: state.users[0]),
feedback: UserCard(user: state.users[0]),
childWhenDragging: (userCount > 1) ?
UserCard(user: state.users[1]): Container(),
onDragEnd: (drag) {
if (drag.velocity.pixelsPerSecond.dx < 0) {
context.read<SwipeBloc>()
.add(SwipeLeft(user: state.users[0]));
print('Swiped left');
} else {
context.read<SwipeBloc>()
.add(SwipeRight(user: state.users[0]));
print('Swiped right');
}
},
),
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 8.0,
horizontal: 60,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
InkWell(
onTap: () {
context.read<SwipeBloc>()
.add(SwipeLeft(user: state.users[0]));
print('Swiped left');
},
child: ChoiceButton(
width: 60,
height: 60,
size: 25,
hasGradient: false,
color: Theme.of(context).colorScheme.secondary,
icon: Icons.clear_rounded,
),
),
InkWell(
onTap: () {
context.read<SwipeBloc>()
.add(SwipeRight(user: state.users[0]));
print('Swiped right');
},
child: const ChoiceButton(
width: 80,
height: 80,
size: 30,
hasGradient: true,
color: Colors.white,
icon: Icons.coffee,
),
),
ChoiceButton(
width: 60,
height: 60,
size: 25,
hasGradient: false,
color: Theme.of(context).primaryColor,
icon: Icons.watch_later,
),
],
),
),
],
);
}
),
);
}
}
And the error message is:
The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was:
BlocBuilder<SwipeBloc, SwipeState>
If I remove length from var usercount = state.users.length; => var usercount = state.users; then it gives me error is:
The method '[]' was called on null.
Receiver: null
Tried calling: [](0)
The relevant error-causing widget was:
BlocBuilder<SwipeBloc, SwipeState>
Solution 1:[1]
Do this,
var usercount = (state.users ?? []).length;
and its good to have checks, before doing this state.users[0]
check if the list has values like
if(state.users.isNotEmpty){
state.users[0];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mohan Sai Manthri |
