安装Google API日历时出现dart错误

时间:2019-01-31 06:43:03

标签: dart flutter

我将显示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'在父类型中总是抽象的

有人可以帮我吗?

3 个答案:

答案 0 :(得分:1)

如果您的代码基于How to use Google API in flutter?,则会看到我的代码中有@override Future<StreamedResponse> send(...)

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

这就是错误消息的内容。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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);});
});
}

对我有用。