为什么我的代码返回null?……我需要快照才能返回地图列表。……但是它说没有nosuchmethod错误

时间:2019-10-04 07:52:17

标签: flutter dart

为什么我的代码返回null?......我需要快照才能返回地图列表....但是它说没有nomethod方法错误。我试图让switch函数从在开发的列表中获取网址未来的构建者中的未来。有没有原因使未来不返回任何值...在这种情况下为列表?

import re
text = r"./[\folder]/this-is-a.test/fi^le.cxx.LAST[]^\/-.h"
text = re.sub(r'([][^\\./-])(?=.*\1)', '', text, flags=re.S)
print(text)

1 个答案:

答案 0 :(得分:0)

您正在尝试将项目添加到runningShow列表中,该列表为null。您需要像这样初始化列表:

List<Map<String, String>> runningShow = [];

编辑:

好吧,另一个问题是_switchFunction不返回任何内容(您使用Timer.periodic,但我认为您不能像这样从计时器回调中返回值)。

首先,我将ScreenTimer更改为StatefulWidget,因此我们可以使用setState来重建小部件,并可以在dispose方法中取消计时器。计时器现在仅调用setState,将重建小部件并调用_switchFunction。您还可以使用if-else代替switch->较短的代码。

以下是完整示例:

import 'dart:async';
import 'dart:convert';

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

void main() {
  return runApp(
    ScreenTimer(),
  );
}

class ScreenTimer extends StatefulWidget {
  @override
  _ScreenTimerState createState() => _ScreenTimerState();
}

class _ScreenTimerState extends State<ScreenTimer> {
  Timer timer;
  Future<List<Map<String, String>>> runningShow;

  @override
  void initState() {
    super.initState();
    runningShow = _getScreen1();
    timer = Timer.periodic(
      Duration(seconds: 10),
      (_) => setState(() {}),
    );
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  Future<List<Map<String, String>>> _getScreen1() async {
    List<Map<String, String>> runningShow = [];
    var response1 = await http
        .get('https://xxxx.firebaseio.com/Shows/Morning_Show/Screen1.json');
    final String morningResponse = json.decode(response1.body);
    runningShow.add({'url1': morningResponse});
    var response2 = await http
        .get('https://xxxx.firebaseio.com/Shows/Mid_morning/Screen1.json');
    final String midResponse = json.decode(response2.body);
    runningShow.add({'url2': midResponse});
    var response3 = await http
        .get('https://xxxx.firebaseio.com/Shows/Kiss_Drive/Screen1.json');
    final String driveResponse = json.decode(response3.body);
    runningShow.add({'url3': driveResponse});
    var response4 = await http
        .get('https://xxxx.firebaseio.com/Shows/Maloko_Show/Screen1.json');
    final String malokoResponse = json.decode(response4.body);
    runningShow.add({'url4': malokoResponse});

    return runningShow;
  }

  String _switchFunction(List<Map<String, String>> runningShow) {
    int timeHour = DateTime.now().hour;
    if (timeHour >= 0 && timeHour < 10) {
      // 0 - 9
      return runningShow[0]['url1'];
    } else if (timeHour >= 10 && timeHour < 15) {
      // 10 - 14
      return runningShow[1]['url2'];
    } else if (timeHour >= 15 && timeHour < 19) {
      // 15 - 18
      return runningShow[2]['url3'];
    } else {
      // 19 - 23
      return runningShow[3]['url4'];
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: Text('Screen1')),
        body: FutureBuilder(
          future: runningShow,
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
            print(snapshot);
            switch (snapshot.connectionState) {
              case ConnectionState.none:
              case ConnectionState.waiting:
                return Image.asset('assets/default.jpg');
              case ConnectionState.done:
                if (snapshot.hasError) {
                  return Text('Error:${snapshot.error}');
                } else {
                  print('Fetched Urls are:${snapshot.data}');
                  var url = _switchFunction(snapshot.data);
                  return Image.network(
                    url,
                    fit: BoxFit.fill,
                    height: double.infinity,
                    width: double.infinity,
                  );
                }
                break;
              default:
                Card(
                  child: Center(
                    child: Text('No Images to Display'),
                  ),
                );
            }
            return Container(width: 0.0, height: 0.0);
          },
        ),
      ),
    );
  }
}