flutter: 'Image'不是'String'类型的子类型 错误。我可以做什么?

问题描述 投票:0回答:1

我试图根据时间改变我的应用程序的背景,但我得到一个错误。当我只是硬编码我想要的特定背景,它的工作。我的代码如下。

我的代码末尾的方法检查它是'上午'还是'下午',然后相应地改变背景.该方法是从第一个容器部件内部调用的。

注意:我是个新手。

代码:

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

void main() => runApp(WeatherApp());

class WeatherApp extends StatefulWidget {
  @override
  _WeatherAppState createState() => _WeatherAppState();
}

class _WeatherAppState extends State<WeatherApp> {
  var apiKey = '5f10958d807d5c7e333ec2e54c4a5b16';
  var description;
  var temp;
  var city;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Container(
        decoration: BoxDecoration(
          image: DecorationImage(image: AssetImage(displayBackground()
          ),
          ),
        ),
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Column(
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(top: 80),
                child: Text(
                  'Your Location',
                  style: TextStyle(fontSize: 20, color: Colors.white),
                ),
              ),
              Container(
                color: Colors.transparent,
                margin: EdgeInsets.symmetric(horizontal: 50),
                child: Row(
                  children: <Widget>[
                    Container(
                      child: Text(
                        'City Name',
                        style: TextStyle(fontSize: 30, color: Colors.white),
                      ),
                    ),
                    SizedBox(width: 50),
                    Container(
                      child: Padding(
                        padding: const EdgeInsets.all(16.0),
                        child: Icon(
                          Icons.location_on,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Card(
                color: Colors.transparent,
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ListTile(
                    leading: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Icon(
                        Icons.wb_sunny,
                        color: Colors.white,
                      ),
                    ),
                    title: Center(
                      child: Text(
                        'Temperature',
                        style: TextStyle(fontSize: 20, color: Colors.white),
                      ),
                    ),
                    subtitle: Center(
                      child: Text(
                        'Weather description',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  displayBackground(){
    var now = DateTime.now();
    final currentTime = DateFormat.jm().format(now);
    if(currentTime.contains('AM')){

      return Image.asset('images/BloodTime.jpg');
    }else if(currentTime.contains('PM')){
      return Image.asset('images/Sun.jpg');
    }
  }
}

错误:

Performing hot reload...
Syncing files to device Android SDK built for x86...

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building WeatherApp(dirty, state: _WeatherAppState#44045):
type 'Image' is not a subtype of type 'String'

The relevant error-causing widget was: 
  WeatherApp file:///C:/Users/aldo0/Desktop/Learn_Flutter/my_weather_app/lib/main.dart:7:23
When the exception was thrown, this was the stack: 
#0      _WeatherAppState.build (package:com/main.dart:25:52)
#1      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
#2      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
#3      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 1 of 623 libraries in 459ms.
flutter dart flutter-layout
1个回答
1
投票

你必须从displayBackground方法返回字符串。

   displayBackground(){
      var now = DateTime.now();
       final currentTime = DateFormat.jm().format(now);
           if(currentTime.contains('AM')){

               return 'images/BloodTime.jpg'; // change
            }else if(currentTime.contains('PM')){
                 return 'images/Sun.jpg'; // change
         }
    } 
© www.soinside.com 2019 - 2024. All rights reserved.