问题描述
'package: Flutter/src/widgets/basic.dart': 断言失败:第 7419 行 pos 15: 'child != null': 不是真的。也可以看看: http://flutter.dev/docs/testing/errors
我不明白为什么会这样。
pubspec.yaml
google_maps_Flutter: "^2.0.1"
Flutter_google_places: "^0.3.0"
home.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),],)),);
}
也无法在列表视图上填充 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)';
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)