错误:NoSuchMethodError:在 null 上调用了方法“[]”。接收者:null 尝试调用:[]("name")

时间:2021-04-29 09:11:34

标签: flutter

我是软件开发新手,我正在使用 Flutter 并尝试从后端检索文档,但出现错误。 下面是我的代码...

import 'dart:convert';
import 'dart:io';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:get_it/get_it.dart';
import 'package:http/http.dart';
import 'package:path_provider/path_provider.dart';

import '../rest_ds.dart';

class DocumentsHandler{

  RestDatasource _restDatasource = GetIt.I<RestDatasource>();

  Future<List<DownloadTask>> getDownloads() async {
    return await FlutterDownloader.loadTasks();
  }

  Future<DownloadTask> getDownloadByName(String filename) async {
    String query = "SELECT * FROM task WHERE file_name = '$filename'";
    List<DownloadTask> downloads = await FlutterDownloader
    .loadTasksWithRawQuery(query: query);

    if(downloads.length > 0){
     return downloads.removeLast();
  }

  return null;
}

 Future<List> getDocumentsOnline() async {
   Response response =  await _restDatasource.getDocuments();
    return jsonDecode(response.body);
}

Future<List<DownloadTask>> loadDocuments() async {
  List onlineDocuments = await getDocumentsOnline();
  Directory externalStorageDirectory = await getExternalStorageDirectory();

 /*onlineDocuments.forEach((document) async {

  });*/

  for(int i = 0; i < onlineDocuments.length; i++){
    var document = onlineDocuments.elementAt(i);
    DownloadTask downloadTask = await getDownloadByName(document["document"]["name"]);
   if( downloadTask == null){
    print("Enqueue");
    await FlutterDownloader.enqueue(
        url: _restDatasource.buildFileDownloadUrl(document["document"]["id"]),
        savedDir: externalStorageDirectory.absolute.path,
        fileName: document["document"]["name"],
        showNotification: true,
        requiresStorageNotLow: false
      );
    }
  }
   return this.getDownloads();
  }
}

下面是我在模拟器上遇到的错误。究竟什么是 null,因为我似乎无法弄清楚它是什么?

enter image description here

请帮忙!!!

0 个答案:

没有答案