如何在 Flutter 中显示 Future 结果(错误)

How to display Future result (error) in Flutter

我正在尝试使用 Places API 构建地址搜索。但是我不知道怎么显示结果。

搜索字段class

import 'package:cloud_firestore/cloud_firestore.dart';
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 = '';
        }, 
        icon: Icon(Icons.clear))
    ];
  }

  @override
  Widget? buildLeading(BuildContext context) {
    // TODO: implement buildLeading
    return 
      IconButton(
        onPressed: (){
          //close(context, null);
        }, 
        icon: Icon(Icons.arrow_back));
  }

  @override
  Widget buildResults(BuildContext context) {
    // TODO: implement buildResults
    return Center(child: Text("Adresse nicht gefunden"));
  }

  PlaceApiProvider result = PlaceApiProvider(Client);
  @override
  Widget buildSuggestions(BuildContext context) {
    print('counter value :');
    final PlaceApiProvider requests = PlaceApiProvider(PlaceApiProvider);
    // TODO: implement buildSuggestions
    return FutureBuilder(
      future: query == ""
          ? null
          : result.fetchSuggestions(),
      builder: (context, snapshot) => query == ''
      ? Container(
        padding: EdgeInsets.all(16),
        child: Text("Gib deine Unternehmensadresse an"),
      )
      : snapshot.hasData
        ? ListView.builder(
          itemBuilder: (context, index) => ListTile(
          //data = snapshot.data[index] as Suggestion;
            //Data return here
            title: 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]”的左括号说:

the method []' can't be unconditionally invoked because the receiver can be 'null'

title: Text((snapshot.data[index] as Suggestion).description),

如果我将其更改为: title: Text((snapshot.data?[index] as Suggestion).description), 颤振说:

the operator ' []' isn't defined for the type 'object'. try defining the operator '[]'

API 调用 class

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;

  Suggestion(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') {
          
        //Compose Suggestions in a List
        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();
  }


}```

The API does work and returns a json, but I don't know how to get the descriptions or the placeId. 

I am on this for days and would be so thankful if somebody can help me.

您需要做的是:

final suggestion = snapshot.data as Suggestion?;

你没有地方写这一行的原因是因为代码写得不好。通过将它分解成小部件来重构它,写得很好的“if”语句,然后找出问题所在。