我正試圖用Places API建立一個地址搜索。但我不知道如何顯示搜索結果。
Searchfield類
import 'package:cloud_firestore/cloud_firestore.dart'/span>。
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:loci/services/place_service.dart'。
class addressSearch extends SearchDelegate< Suggestion> {
searchAddress(sessionToken) {
apiClient = PlaceApiProvider(sessionToken)。
}
PlaceApiProvider? apiClient;
@override
List<Widget>? buildActions(BuildContext context) {
// TODO: implement buildActions。
return [
IconButton(
onPressed: (){
query = ''/span>;
},
icon。Icon(Icons.clear))
];
}
@override 小工具?
Widget? buildLeading(BuildContext context) {
// TODO: implement buildLeading。
return
IconButton(
onPressed: (){
//close(context, null);
},
圖示。Icon(Icons.arrow_back))。
}
@override
小工具 buildResults(BuildContext context) {
// TODO: 實作 buildResults。
return Center(child: Text("Adresse nicht gefunden"))。)
}
PlaceApiProvider result = PlaceApiProvider(Client);
@override
小工具 buildSuggestions(BuildContext context) {
print('counter value :')。
final PlaceApiProvider requests = PlaceApiProvider(PlaceApiProvider)。
// TODO: implement buildSuggestions
return FutureBuilder(
future: query == "" ?
? null ?
: result.fetchSuggestions()。
建設者。(context, snapshot) => query == '' ?
? 容器(
padding: EdgeInsets.all(16)。
孩子。Text("Gib deine Unternehmensadresse an") 。
)
: snapshot.hasData
? ListView.builder(
itemBuilder。(context, index) => ListTile(
//data = snapshot.data[index] as Suggestion;/span>
//資料在此回傳。
標題。Text((snapshot.data[index] as Suggestion).description)。
onTap: () {
close(context, snapshot.data as Suggestion) 。
},
),
itemCount: (snapshot.data as dynamic).length。
)
: Center(child: Text('l?dt...') )。
);
}
}
但是flutter在"[index]"的開頭括號中標記說:
方法[]'不能被無條件地呼叫,因為接收器 可以是'null'
title: Text((snapshot.data[index] as Suggestion).description),
如果我把它改成:
title: Text((snapshot.data?[index] as Suggestion).description),/code>
Flutter說:
運算子'[]'沒有為'object'型別定義。 運算子'[]'
API呼叫類
import 'dart:convert'。
import 'dart:developer';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http/http.dart'。
class Suggestion{
final String placeId;
final String description;
建議(this.placeId, this.description)。
@override
String toString() {
return 'Suggestion(description: $description, placeId: $placeId'。
}
}
class PlaceApiProvider{
final client = new Client()。
PlaceApiProvider(this.sessionToken)。
final sessionToken。
// static final String androidKey = 'XYZ';
// static final String iosKey = 'XYZ';
// final apiKey = Platform.isAndroid ? androidKey : iosKey;
Future<List<Suggestion> > fetchSuggestions() async {
print(' counter value : RESPONSE')。
const request = 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=TEST&types=address& components=country:de&key=XYZ'。
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<Suggestion>((p) => Suggestion(p['place_id'], p['description'))
.toList()。
}
if (result['status'] == 'ZERO_RESULTS') {
print('keine ergebnisse')。
return【】。
}
print('error')。
throw Exception(result['error_message'])。
} else {
print('fehler beim laden')。
throw Exception('Failed to fetch suggestion') 。
}
// throw ArgumentError.notNull();
}
}```
該API確實作業,并回傳一個json,但我不知道如何獲得描述或placeId。
我已經研究了好幾天了,如果有人能幫助我,我將非常感激。
uj5u.com熱心網友回復:
你需要做的是:
final suggestion = snapshot.data as Suggestion?
你沒有地方寫這一行的原因,是因為代碼寫得很差。通過將其分解成小部件和寫得很好的 "if "陳述句來重構它,然后找出問題所在。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/307046.html
標籤:
