【问题标题】:flutter dart error when installing google api calendar安装google api日历时出现飞镖错误
【发布时间】:2019-06-24 13:43:11
【问题描述】:

我将显示来自 Google 日历的事件列表。 我已经按照以下链接中的示例进行了操作:How to use Google API in flutter?

我的脚本如下:

import 'package:http/http.dart' as http;

假设我已登录。

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: <String>[
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
    'https://www.googleapis.com/auth/calendar'
  ],
);'
class GoogleHttpClient extends http.BaseClient {
  Map<String, String> _headers;

  GoogleHttpClient(this._headers) : super();

  @override
  Future<http.StreamedResponse> send(http.BaseRequest request) =>
      super.send(request..headers.addAll(_headers)); //have error 'the method 'send' is always abstract in the supertype'

  @override
  Future<http.Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));
}
void getCalendarEvents() async {
    final authHeaders = _googleSignIn.currentUser.authHeaders;
    final httpClient = new GoogleHttpClient(authHeaders); //have error "The argument type 'Future<Map<String, String>>' can't be assigned to the parameter type 'Map<String, String>'"
    var calendar = new Calendar.CalendarApi(new http.Client());
    var calEvents = calendar.events.list("primary");
    calEvents.then((Calendar.Events events) {
      events.items.forEach((Calendar.Event event) {print(event.summary);});
    });
}

由于错误,上述脚本无法运行。

方法 'send' 在超类型中总是抽象的

有人可以帮我吗?

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    如果您的代码基于How to use Google API in flutter?,您会看到我的代码中有一个@override Future&lt;StreamedResponse&gt; send(...)

    GoogleHttpClient 扩展了abstract class IOClient 缺少send 的实现,因此具体的子类需要实现它。

    这就是错误消息的内容。

    【讨论】:

    • 感谢您的快速回答。我如何实现抽象类IOClient 本身?我只是将其定义如下:import 'package:http/http.dart' as http; 谢谢,
    • 对不起,我不明白评论。请检查stackoverflow.com/questions/48477625/…中答案中的代码
    【解决方案2】:

    将 StreamedResponse 替换为 IOStreamedResponse 添加 IOClient 库 将class GoogleHttpClient extends IOClient 替换为class GoogleHttpClient extends http.BaseClient

    【讨论】:

      【解决方案3】:

      1 这是错误

      //have error "The argument type 'Future<Map<String, String>>' can't be assigned to the parameter type 'Map<String, String>'"
      

      固定:在前面添加等待

      如下:

      final authHeaders = await _googleSignIn.currentUser.authHeaders;
      

      2:改变如下

      var calendar = new Calendar.CalendarApi(new http.Client());
      

      var calendar = new Calendar.CalendarApi(httpClient);
      

      ======> 最终结果:

      void getCalendarEvents() async {
      final authHeaders = await _googleSignIn.currentUser.authHeaders;
      final httpClient = new GoogleHttpClient(authHeaders);
      var calendar = new Calendar.CalendarApi(httpClient);
      var calEvents = calendar.events.list("primary");
      calEvents.then((Calendar.Events events) {
        events.items.forEach((Calendar.Event event) {print(event.summary);});
      });
      }
      

      它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多