Flutter app error "NoSuchMethodError: The getter 'length' was called on null" - api

Am getting this error and can't find the why, the app shows a google map calling hospital from google map places api, api call returns fine when used in chrome, but the app fail to build showing the "NoSuchMethodError: The getter 'length' was called on null" error. Thanks for your help.
class Search extends StatelessWidget {
#override
Widget build(BuildContext context) {
final currentPosition = Provider.of<Position>(context);
final placesProvider = Provider.of<Future<List<Place>>>(context);
return FutureProvider(
create: (context) => placesProvider,
child: Scaffold(
body: (currentPosition != null)
? Consumer<List<Place>>(
builder: (_, places, __) {
return Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 3,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(currentPosition.latitude,
currentPosition.longitude),
zoom: 16.0),
zoomGesturesEnabled: true,
mapToolbarEnabled: true,
myLocationEnabled: true,
scrollGesturesEnabled: true,
),
),
SizedBox(
height: 10.0,
),
Expanded(
child: ListView.builder(
itemCount: places.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(places[index]?.name),
),
);
}),
)
],
);
},
)
: Center(
child: CircularProgressIndicator(),
),
),
);
}
}
The api request works fine when called in chrome, problem is when building the app. This is my places_service.dart file:
class PlacesService {
final key = 'xxxxxxxxxxxxxxxxxxxxx-ttttttttttttttttt';
Future<List<Place>> getPlaces(double lat, double lng) async {
var response = await http.get('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$lng&type=hospital&rankby=distance&key=$key');
var json = convert.jsonDecode(response.body);
var jsonResults = json['results'] as List;
return jsonResults.map((place) => Place.fromJson(place)).toList();
}
}
Problem is this line:
itemCount: places.length,

If you use constructions like places.length the programming code can fail on two places:
when places = null
when places.length = null (your situation)
You cannot call methods and properties on null-objects; that is why you get this error. So before you call places.length you must be sure that both situations cannot occur. How this is done and if the check is really necessary depends on the rest of your program. One way to check is:
itemCount: places.length != null ? places.length : 0;

Related

Flutter: A non-null String must be provided to a Text widget. Failed assertion: line 378 pos 10: 'data != null'

I getting data from API and adding it to data table, it's working fine, when i select month from dropdown, my data table get modify according to selected month from drop down, but initially it print this error.
and its change to this when i select month from dropdown
here is code
class MyAttendance extends StatefulWidget {
#override
_MyAttendanceState createState() => _MyAttendanceState();
}
class _MyAttendanceState extends State<MyAttendance> {
//List<History> _historyList;
List<History> historyList=[];
String _selectedLeave;
int monthIndex;
int month;
var monthsList=<String>[
'January',
'Febuary',
'March',
'April',
'May',
'June',
'July',
'Augest',
'September',
'October',
'November',
'December'
];
String getdate="";
void _getDate() {
final String formattedDateTime =
DateFormat('MM').format(DateTime.now()).toString();
setState(() {
getdate = formattedDateTime;
print("date "+getdate);
});
}
_userDetails() async{
SharedPreferences myPrefs=await SharedPreferences.getInstance();
setState(() {
getname=myPrefs.getString('name');
});
}
void initState() {
_userDetails();
_getDate();
_getRecord();
}
Future<List<History>> _getRecord() async{
Dio dio=new Dio();
var data={
'username':getname,
'month':month
};
return dio
.post(localhostUrlAttendanceHistory,data: json.encode(data))
.then((onResponse) async {
var jsonData=onResponse.data['data'];
//List<History> historyList = [];
for (var h in jsonData) {
History history = History(
h["Date"],
h["TimeIn"],
h["TimeOut"],
);
historyList.add(history);
}
return historyList;
})
.catchError((onerror){
print(onerror.toString());
});
}
Widget attendanceHistory(List<History>
historyList)=>
DataTable(columns: <DataColumn>[
DataColumn(label: Text("Date"),),
DataColumn(label: Text("Time in" ),),
DataColumn(label: Text("Time out")
)],
rows:
historyList
?.map((element)=>DataRow(
cells: <DataCell>[
DataCell(Text(element?.date)),
DataCell(Text(element?.timeIn)),
DataCell(Text(element?.timeOut)),
]) )?.toList());
TextEditingController fromDate=new TextEditingController();
TextEditingController toDate=new TextEditingController();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: new MyAppBar(title: Text("My Attendance"),onpressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>Profile()));
}),
drawer:Emp_DrawerCode(),
body:Stack(children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(25, 15, 0, 0),
child: Text("Attendance history",style: TextStyle(fontSize: 30,color: Colors.blue[900],fontWeight: FontWeight.bold),),
),
Container(
padding: EdgeInsets.fromLTRB(45, 80, 10, 0),
child:
DropdownButton<String>(
value: _selectedLeave==null?null:monthsList[monthIndex],
items:
monthsList
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value)
);
}).toList(),
hint:Text(
"Please choose a month",
),
onChanged: (String value) {
setState(() {
_selectedLeave=value;
monthIndex = monthsList.indexOf(value);
month=monthIndex+1;
print(month);
print(_selectedLeave);
});
},
),
),
Container(
padding: EdgeInsets.fromLTRB(15, 150, 0, 0),
child:SingleChildScrollView(
scrollDirection: Axis.vertical,
child: FutureBuilder(
future: _getRecord(),
builder: (BuildContext context, AsyncSnapshot<List<History>> snapshot) {
// Check if the data has been received.
if (snapshot.hasData) {
// Return the widget and provide the received data.
return attendanceHistory(snapshot.data);
}
return Center(child: CircularProgressIndicator(),);
// print("data");
// print(snapshot.data);
// Text("No data is shown");
//return attendanceHistory(snapshot.data);
}
),
),)
]));
}
}
class History {
final String date;
final String timeIn;
final String timeOut;
History(this.date, this.timeIn, this.timeOut);
}
Error
════════ Exception caught by widgets library ═══════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 378 pos 10: 'data != null'
The relevant error-causing widget was
FutureBuilder<List<History>>
lib\My_Attendance\MyAttendance.dart:171
Update:
After few seconds of red screen error, it display this, and i have not select month from dropdown too.
and it sends this error now
════════ Exception caught by widgets library ═══════════════════════════════════
The method 'map' was called on null.
Receiver: null
Tried calling: map<DataRow>(Closure: (History) => DataRow)
The relevant error-causing widget was
FutureBuilder<List<History>>
Update
Error:
Update:
builder: (BuildContext context, AsyncSnapshot<List<History>> snapshot) {
// Check if the data has been received.
if(snapshot.connectionState==ConnectionState.done){
if (snapshot.hasData) {
return Center(
child: Text('${snapshot.error} occured',
style: TextStyle(fontSize: 18)),
);
}
else if(snapshot.hasData){
return attendanceHistory(snapshot.data);
}
// Return the widget and provide the received data.
//return Center(child: CircularProgressIndicator(),);
}
//return attendanceHistory(snapshot.data);
return Center(child: CircularProgressIndicator(),);
// print("data");
// print(snapshot.data);
// Text("No data is shown");
}
),
kindly please help how i can fix it?
A better practice is to use loader while fetching the data from API. Since future builder has a property to check whether there is response from API or not. TO check this simply use
`FutureBuilder(
future: _getRecord(),
builder: (BuildContext context,
AsyncSnapshot<List<History>> snapshot) {
// Below line will check whether future has data or not
if (snapshot.connectionState == ConnectionState.done) {
// If we got an error
if (snapshot.hasError) {
return Center(
child: Text(
'${snapshot.error} occured',
style: TextStyle(fontSize: 18),
),
);
// if we got our data
} else if (snapshot.hasData) {**strong text**
if (snapshot.hasData) {
return attendanceHistory(snapshot.data);
}
return Center(child: CircularProgressIndicator(),);
}`
Or else you can use null aware operator to handle the null value as below.
Widget attendanceHistory(List<History>
historyList)=>
DataTable(columns: <DataColumn>[
DataColumn(label: Text("Date"),),
DataColumn(label: Text("Time in" ),),
DataColumn(label: Text("Time out")
)],
rows:
historyList
?.map((element)=>DataRow(
cells: <DataCell>[
DataCell(Text(element?.date)),
DataCell(Text(element?.timeIn)),
DataCell(Text(element?.timeOut)),
])
)?.toList());
This error generate when you pass a null value to Text widget. So to avoid this either you can put a null check condition before displaying text or you can pass a default value to the Text widget.
Widget attendanceHistory(List<History> historyList)=>
DataTable(columns: <DataColumn>[
DataColumn(label: Text("Date"),),
DataColumn(label: Text("Time in" ),),
DataColumn(label: Text("Time out")
)],
rows:
historyList
.map((element)=>DataRow(
cells: <DataCell>[
DataCell(Text(element.date ?? "N/A")),
DataCell(Text(element.timeIn ?? "N/A")),
DataCell(Text(element.timeOut ?? "N/A")),
])
).toList());
Display CircularProgressIndicator while fetching the records.
Container(
padding: EdgeInsets.fromLTRB(15, 150, 0, 0),
child:SingleChildScrollView(
scrollDirection: Axis.vertical,
child: FutureBuilder( //This is line 171
future: _getRecord(),
builder: (BuildContext context, AsyncSnapshot<List<History>> snapshot) {
// Check if the data has been received.
if (snapshot.hasData) {
// Return the widget and provide the received data.
return attendanceHistory(snapshot.data);
}
return Center(child: CircularProgressIndicator(),);
}
),

API data display - flutter card widget

Get API connection is successful. but I have no idea how to map data.. how is connect snapshot.data,
I want to display data in a Stack widget.
https://flutter.dev/docs/cookbook/networking/fetch-data
I learned this from the association with this source
Future<News> fetchNews() async {
final response =
await http.get('#url');
print(response.body);
if (response.statusCode == 200) {
return News.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load news');
}
}
class News {
final String title;
final String description;
final String image;
News({this.title, this.description, this.image});
factory News.fromJson(Map<String, dynamic> json) {
return News(
title: json['title'],
description: json['description'],
image: json['image'],
);
}
}
Widget _buildPage(BuildContext context, int direction) {
Size size = MediaQuery.of(context).size;
return SafeArea(
child: FutureBuilder<News>(
future: futureNews,
builder: (context, snapshot) {
if (snapshot.hasData) {
return newsCard(snapshot.data);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
),
);
}
Widget newsCard(size){
return Stack(
children: <Widget>[
Container(
child: Image.network(
image,
fit: BoxFit.cover
),
height: size.height/2.0,
),
),
],
);
}
Your code is a bit messy and it doesn't make much sense. It seems obvious that the data is not getting displayed because you're passing your News object to your newsCard function, but the parameter seems to be expecting Size. I also don't know where are you getting the image property from since it doesn't seem to be declared anywhere in the code.
Doing some changes to your code, a minimal working function that returns your widget would look like this:
Widget _buildPage(BuildContext context, int direction) {
Size size = MediaQuery.of(context).size;
return SafeArea(
child: FutureBuilder<News>(
future: futureNews,
builder: (context, snapshot) {
if (snapshot.hasData) {
return newsCard(snapshot.data, size);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
),
);
}
Widget newsCard(News news, Size size){
return Stack(
children: <Widget>[
Container(
child: Image.network(
news.image,
fit: BoxFit.cover
),
height: size.height/2.0,
),
),
],
);
}
UPDATE
If you're getting the error you mentioned bellow, is likely that your response from the get request is a list of News and not just one News object, therefore you'll have to change this:
return News.fromJson(jsonDecode(response.body));
To this:
return jsonDecode(response.body).map<News>((object) => News.fromJson(object)).toList();
The return type of fetchNews() should be Future<List<News>> instead of Future<News>

Flutter: Snapshot isn't showing data

I create a Listview builder with get method (API call). API call is fine, cause I get response. But in widget snapshot.data show me null. I can't fixed this problem and don't why it's behaving like this. Please someone help me.
API responsebody
Here is my code for API call
class APIService {
Future<List<EducationInfo>> getEducationInfo() async {
String url = "$baseAPIUrl/educations";
String _token = await SavedData().loadToken();
String authorization = "Bearer $_token";
var response = await http.get(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
"Authorization": authorization
});
print('API ${response.statusCode}\n API${json.decode(response.body)}');
if (response.statusCode == 200) {
var jsonResponse = response.body;
var decoded = json.decode(jsonResponse);
return decoded['success']
.map<EducationInfo>((b) => EducationInfo.fromJson(b))
.toList();
} else {
throw Exception('Failed to load Education Information');
}
}
}
Here is my Model.dart
//Model
class EducationInfo {
int id;
String degreeName;
int rollNumber;
int regNumber;
int passingYear;
String gradeType;
double cgpa;
double outOfCgpa;
String divisionName;
String groupName;
String subjectName;
String boardName;
String instituteName;
EducationInfo({
this.id,
this.degreeName,
this.rollNumber,
this.regNumber,
this.passingYear,
this.gradeType,
this.cgpa,
this.outOfCgpa,
this.divisionName,
this.groupName,
this.subjectName,
this.boardName,
this.instituteName,
});
factory EducationInfo.fromJson(Map<String, dynamic> json) {
return EducationInfo(
id: json['user_id'],
degreeName: json['degree_name'],
rollNumber: json['roll_number'],
regNumber: json['registration_number'],
passingYear: json['passing_year'],
gradeType: json['grade_type'],
cgpa: json['cgpa'],
outOfCgpa: json['out_of_cgpa'],
divisionName: json['division'],
groupName: json['group_name'],
subjectName: json['subject_name'],
boardName: json['board'],
instituteName: json['institute_name'],
);
}
}
And here is my main code-
class Resume extends StatefulWidget {
#override
_ResumeState createState() => _ResumeState();
}
class _ResumeState extends State<Resume> {
Future<List<EducationInfo>> furuteEducationInfo;
#override
void initState() {
super.initState();
furuteEducationInfo = APIService().getEducationInfo();
}
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
automaticallyImplyLeading: false,
leading: IconButton(
icon: Icon(
Icons.arrow_back_ios,
),
onPressed: () {
Navigator.pop(context);
},
),
title: Text("Resume"),
),
body: Align(
child: FutureBuilder(
future: furuteEducationInfo,
builder: (context, snapshot) {
var educationInfo = snapshot.data;
if (snapshot.data == null) {
return Text("No Data Available ");
} else if (snapshot.hasData) {
return ListView.builder(
scrollDirection: Axis.vertical,
itemCount: educationInfo.length,
itemBuilder: (context, index) {
var eduInfo = educationInfo[index];
print(
"\nEducation Info ${educationInfo[index]}");
return designedContainer(
_width - 30,
Padding(
padding: EdgeInsets.all(5.0),
child: Stack(
children: [
Container(
child: Column(
children: [
detailsField(
"Degree Name",
"${_checkNull(eduInfo.degreeName)}"),
],
),
),
Align(
alignment:
Alignment.topRight,
child: editButton(
Icons.add, "Delete",
() {
print("Delete");
}),
)
],
),
));
});
} else {
Text("Something Went to Wrong");
}
}),
),
);
}
And here is also postman Screenshot-
in your EducationInfo.fromJson method
replace
cgpa: json['cgpa'] ?? 0.0,
outOfCgpa: json['out_of_cgpa'] ?? 0.0,
Since it is a future and runs Asynchronously you have to check if the snapshot has data, and wait for the data to be fetched. Following a snippet code how you would check for that inside your build method:
(snapshot.hasData && snapshot.data.length == 0)
? Container(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Congrats, No jobs asssigned!!!")
],
),
),
)
: (snapshot.hasData)
? Container(
child: ListView.builder(
.....)

Why doesn't my FutureBuilder return any data?

I have a api that returns a list of data.
When I retrieve that data I use a FutureBuilder to show the list of data.
But for some reason it won't show my data even though when I print the response I can see that I got a correct response.
This is the error that i got:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (11846): The following assertion was thrown building FutureBuilder<List<BasicDiskInfo>>(dirty, state:
I/flutter (11846): _FutureBuilderState<List<BasicDiskInfo>>#a0948):
I/flutter (11846): A build function returned null.
I/flutter (11846): The offending widget is: FutureBuilder<List<BasicDiskInfo>>
I/flutter (11846): Build functions must never return null. To return an empty space that causes the building widget to
I/flutter (11846): fill available room, return "new Container()". To return an empty space that takes as little room as
I/flutter (11846): possible, return "new Container(width: 0.0, height: 0.0)".
I don't know what to do. Help me?
API
static Future<List<BasicDiskInfo>> fetchAllDisks() async {
final response = await http.get('link');
if (response.statusCode == 200) {
Iterable list = json.decode(response.body);
var disks = new List<BasicDiskInfo>();
disks = list.map((model) => BasicDiskInfo.fromJson(model)).toList();
print(disks[0]);
return disks;
} else {
throw Exception('Failed to load disks');
}
}
Page
class Disks extends StatelessWidget {
#override
Widget build(BuildContext context) {
return FutureBuilder(
future: API.fetchAllDisks(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new CircularProgressIndicator();
default:
if (snapshot.hasError) {
return new Text('Error: ${snapshot.error}');
} else {
print(snapshot.data);
createListView(context, snapshot);
}
}
},
);
}
Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
List<BasicDiskInfo> disks = snapshot.data;
return new ListView.builder(
itemCount: disks.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SpecificDiskPage(
diskId: disks[index].id,
),
));
},
child: Card(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text(disks[index].name),
Spacer(),
Text(disks[index].driveType),
Spacer(),
Text(disks[index].driveFormat),
],
),
Row(
children: <Widget>[
Text(disks[index].totalSize.toString()),
Spacer(),
Text(disks[index].totalFreeSpace.toString()),
],
),
],
),
),
);
},
);
}
}
BasicDiskInfo
class BasicDiskInfo {
int id;
String name;
String driveType;
String driveFormat;
int totalSize;
int totalFreeSpace;
BasicDiskInfo(
{this.id,
this.name,
this.driveType,
this.driveFormat,
this.totalSize,
this.totalFreeSpace});
factory BasicDiskInfo.fromJson(Map<String, dynamic> json) {
return BasicDiskInfo(
id: json['id'],
name: json['name'],
driveType: json['driveType'],
driveFormat: json['driveFormat'],
totalSize: json['totalSize'],
totalFreeSpace: json['totalFreeSpace']);
}
}
The FutureBuilder should return a list with the data from the api
There is an error in your build method. You didn't return createListView(context, snapshot); in your default case.

How to force rebuild list?

I am make random chat app use FirestoreAnimatedList (FirebaseAnimatedList for Firestore).

In general is work.
But there is flicker when show user avatar (Icons.account_circle). For example sometime it show extra Icon (wrong) and then later not show (correct).
So I think maybe is because of Widget rebuild? But I no know how solve.
final Map<int, dynamic> map = {};
...
body: FirestoreAnimatedList(
query: firestore.collection('messages').snapshots(),
itemBuilder: (
BuildContext context,
DocumentSnapshot snapshot,
Animation<double> animation,
int index,
) {
map[index] = snapshot;
return FadeTransition(
opacity: animation,
child: MessageItem(
index: index,
document: snapshot,
map: map,
onTap: _removeMessage,
),
);
},
class MessageItem extends StatelessWidget {
…
#override
Widget build(BuildContext context) {
return Container(
child: new Row(
children: <Widget>[
new Container(
child:
_sameUser() ?
new Icon(
Icons.account_circle
)
:
Container()
bool _sameUser () {
assert(index >= 0);
assert(map != null);
return map[index + 1] != null && map[index + 1]['fromUser'] == map[index]['fromUser'];
}
Anyone help?
Thanks!