flutter - 未处理的异常:'package:flutter/src/widgets/basic.dart':断言失败:第 7419 行 pos 15:'child != null':不正确
问题描述
尝试点击搜索时,抛出此错误:
'package:flutter/src/widgets/basic.dart':断言失败:第 7419 行 pos 15:'child != null':不正确。另见: http: //flutter.dev/docs/testing/errors
我不明白为什么会这样。
发布规范.yaml
google_maps_flutter: "^2.0.1"
flutter_google_places: "^0.3.0"
主页.dart
class HomeScreen extends StatefulWidget {
const HomeScreen({Key key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
final _searchCon = TextEditingController();
@override
void initState() {
// TODO: implement initState
super.initState();
locatePosition();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
key: _scaffoldKey,
drawer: buildDrawer(context),
body: Stack(
children: [
GoogleMap(
onMapCreated: _onMapCreated,
markers: _markers,
onCameraMove: _onCameraMove,
),
TextField(
controller: _searchCon,
readOnly: true,
onTap: () async {
final sessionToken = Uuid().v4();
final Suggestions result = await showSearch(
context: context,
delegate: PlacesSearch(sessionToken),
);
if (result != null) {
setState(() {
_searchCon.text = result.placeDesc;
});
}
},
decoration: InputDecoration(
icon: Container(
margin: EdgeInsets.only(left: 20),
width: 10,
height: 10,
child: Icon(
Icons.search,
color: Colors.black,
),
),
hintText: "Search ....",
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 8.0, top: 16.0),
),
),
],
)),
);
}
也无法在 listview 上填充 Google Places API
class PlacesSearch extends SearchDelegate<Suggestions> {
PlacesSearch(this.sessionToken) {
apiClient = PlaceApiProvider(sessionToken);
}
final sessionToken;
PlaceApiProvider apiClient;
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
tooltip: 'Clear',
icon: Icon(Icons.clear),
onPressed: () => query = '',
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => close(context, null),
);
}
@override
Widget buildResults(BuildContext context) {
return null;
}
@override
Widget buildSuggestions(BuildContext context) {
return FutureBuilder(
future: query.isEmpty
? null
: apiClient.fetchSuggestions(
query, Localizations.localeOf(context).languageCode),
builder: (context, snapshot) => query.isEmpty
? Container(
padding: EdgeInsets.all(16.0),
child: Text('hello'),
)
: snapshot.hasData
? ListView.builder(
itemBuilder: (context, index) => ListTile(
//display data returned from our future
title:
Text((snapshot.data[index] as Suggestions).placeDesc),
onTap: () {
close(context, snapshot.data[index] as Suggestions);
},
),
itemCount: snapshot.data.length,
)
: Container(child: Text('Loading .....')),
);
}
}
class PlaceApiProvider {
final client = Client();
PlaceApiProvider(this.sessionToken);
final sessionToken;
static final String androidKey = 'API_KEY';
static final String iosKey = 'API_KEY';
final apiKey = Platform.isAndroid ? androidKey : iosKey;
Future<List<Suggestions>> fetchSuggestions(String input, String lang) async {
final request =
'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$input&types=address&language=$lang&components=country:ch&key=$apiKey&sessiontoken=$sessionToken';
final response = await client.get(Uri.parse(request));
if (response.statusCode == 200) {
final result = json.decode(response.body);
if (result['status'] == 'OK') {
return result['predictions']
.map<Suggestions>(
(p) => Suggestions(p['place_id'], p['description']))
.toList();
}
if (result['status'] == 'ZERO_RESULTS') {
return [];
}
throw Exception(result['error_message']);
} else {
throw Exception('Failed to fetch suggestion');
}
}
}
建议课
class Suggestions {
final String placeId;
final String placeDesc;
Suggestions(this.placeId, this.placeDesc);
@override
String toString() {
// TODO: implement toString
return 'Suggestions(description: $placeDesc, placeId: $placeId)';
}
}
解决方案
推荐阅读
- python - Pygame 碰撞后删除图像和计分器
- javascript - 对打字稿文件语法错误的开玩笑测试:“接口是严格模式下的保留字”
- c++ - 为什么我看到“与其类同名的成员函数必须是构造函数”?
- javascript - 如何仅更改状态的一部分?
- highcharts - 自定义字幕
- wordpress - 带有查询参数的 Apache 重定向
- node.js - 由于 Webpack 不匹配,npm start 无法正常工作
- c# - 除非用括号括起来,否则 Json 帖子无效
- javascript - geoAlbersUSA 投影未按预期映射
- laravel - 如何清理 Laravel Telescope 调试日志