[英]Can anyone help Beginner to Flutter. The relevant error-causing widget was:
I'm a beginner to flutter development, and working on Search functionality for the app, can anyone guide me why am I getting the following error:我是 flutter 开发的初学者,并且正在开发应用程序的搜索功能,谁能指导我为什么会出现以下错误:
======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building SearchScreen(dirty, state: _SearchScreenState#1ec80): The getter 'length' was called on null.
======== 小部件库捕获的异常====================================== ================== 在构建 SearchScreen(dirty, state: _SearchScreenState#1ec80) 时抛出以下 NoSuchMethodError:在 null 上调用了 getter 'length'。 Receiver: null Tried calling: length
接收方:null 尝试调用:长度
Here is my SearchScreen dart file code:这是我的 SearchScreen dart 文件代码:
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:fluttertravelapp/constants.dart';
import 'package:fluttertravelapp/models/new_category_model.dart';
import 'dart:ui';
import 'package:fluttertravelapp/widgets/bottom_navigation_bar.dart';
import 'package:google_fonts/google_fonts.dart';
class SearchScreen extends StatefulWidget {
@override
_SearchScreenState createState() => _SearchScreenState();
}
class _SearchScreenState extends State<SearchScreen> {
List<NewCategoryModel> _foundedPlaces;
onSearch(String search) {
setState(() {
_foundedPlaces = placesList(search);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
//bottomNavigationBar: BottomNavigationBarTravel(),
body: SafeArea(
child: Container(
child: ListView(
physics: BouncingScrollPhysics(),
children: <Widget>[
/// Custom Navigation Drawer and Search Button
Container(
height: 57.6,
margin: EdgeInsets.only(top: 28.8, left: 28.8, right: 28.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
],
),
),
/// Text Widget for Title
Padding(
padding: EdgeInsets.only(top: 0, left: 28.8),
child: Text(
'Search',
style: GoogleFonts.playfairDisplay(
fontSize: 45.6, fontWeight: FontWeight.w700),
),
),
AppBar(
elevation: 0,
backgroundColor: Colors.white,
title: Container(
height: 48,
child: TextField(
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
onChanged: (value) => onSearch(value),
decoration: InputDecoration(
filled: true,
fillColor: kPrimaryColor,//Colors.grey[850],
contentPadding: EdgeInsets.all(0),
prefixIcon: Icon(Icons.search, color: Colors.white /*.grey.shade500,*/),
suffixIcon: Icon(Icons.clear, color: Colors.white /*.grey.shade500,*/),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(50),
borderSide: BorderSide.none
),
hintStyle: TextStyle(
fontSize: 18,
fontWeight: FontWeight.normal,
color: Colors.white, //.grey.shade500
),
hintText: "Search places"
),
),
),
),
Container(
color: Colors.white,
child: _foundedPlaces.length == null ? ListView.builder(
itemCount: _foundedPlaces.length,
itemBuilder: (context, index) {
return Slidable(
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: placeComponent(places: _foundedPlaces[index]),
actions: <Widget>[
new IconSlideAction(
caption: 'Archive',
color: Colors.transparent,
icon: Icons.archive,
onTap: () => print("archive"),
),
new IconSlideAction(
caption: 'Share',
color: Colors.transparent,
icon: Icons.share,
onTap: () => print('Share'),
),
],
secondaryActions: <Widget>[
new IconSlideAction(
caption: 'More',
color: Colors.transparent,
icon: Icons.more_horiz,
onTap: () => print('More'),
),
new IconSlideAction(
caption: 'Delete',
color: Colors.transparent,
icon: Icons.delete,
onTap: () => print('Delete'),
),
],
);
}) : Center(child: Text("No places found", style: TextStyle(color: Colors.black),)),
),
],
),
),
),
);
}
placeComponent({NewCategoryModel places}) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 20),
padding: EdgeInsets.only(top: 10, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Container(
width: 60,
height: 60,
child: ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.network(places.image),
)
),
SizedBox(width: 10),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(places.placeName, style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500)),
SizedBox(height: 5,),
Text(places.cityName, style: TextStyle(color: Colors.grey[500])),
]
)
]
),
],
),
);
}
}
What I think I'm getting the error here in my if else condition:我认为我在 if else 条件下遇到了错误:
child: _foundedPlaces.length == null ? ListView.builder(
itemCount: _foundedPlaces.length,
itemBuilder: (context, index) {
return Slidable(
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: placeComponent(places: _foundedPlaces[index]),
actions: <Widget>[
new IconSlideAction(
caption: 'Archive',
color: Colors.transparent,
icon: Icons.archive,
onTap: () => print("archive"),
),
new IconSlideAction(
caption: 'Share',
color: Colors.transparent,
icon: Icons.share,
onTap: () => print('Share'),
),
],
secondaryActions: <Widget>[
new IconSlideAction(
caption: 'More',
color: Colors.transparent,
icon: Icons.more_horiz,
onTap: () => print('More'),
),
new IconSlideAction(
caption: 'Delete',
color: Colors.transparent,
icon: Icons.delete,
onTap: () => print('Delete'),
),
],
);
}) : Center(child: Text("No places found", style: TextStyle(color: Colors.black),)),
Kindly help!!!!!!请帮忙!!!!!!!
length couldn't be null.you should change condition this way _foundedPlaces.length ==0 or _foundedPlaces.length <= 0
长度不能是 null。你应该这样改变条件
_foundedPlaces.length ==0 or _foundedPlaces.length <= 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.