0

当我在其中传递 imageURL 变量时,网络图像不会加载。但是,如果我直接在参数内使用 URL,那么它可以正常工作。我该如何解决这个问题,因为我需要从 FirebaseFirestore 获取 URL,所以我必须将它存储在 String 变量中。

这样做很好 -NetworkImage('https://images.ctfassets.net/3s5io6mnxfqz/6ZImCEzx6UuvuKaAiZEDDN/50479ee4a0902deb4eb1bab720ce248a/image1.jpg');

但这不起作用- NetworkImage(imageURL);

class _MainOrderScreenState extends State<MainOrderScreen> {
  final _auth = FirebaseAuth.instance;
  FirebaseFirestore _firestore = FirebaseFirestore.instance;
  
  String imageURL =
      'https://images.ctfassets.net/3s5io6mnxfqz/6ZImCEzx6UuvuKaAiZEDDN/50479ee4a0902deb4eb1bab720ce248a/image1.jpg';

 

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      body: Container(
        padding: EdgeInsets.symmetric(vertical: 20, horizontal: 20),
        child: Column(
          children: [
            Text(
              'currently serving:',
              style: kTiffinzoTitle.copyWith(fontSize: 40),
            ),
            Image(
              image: imageURL != null
                  ? NetworkImage(imageURL)
                  : null,
              width: 200,
            ),
            Container(
              decoration: kButtonStyle,
              child: FlatButton(
                child: Text('Log out'),
                onPressed: () {
                  _auth.signOut();
                  Navigator.pop(context);
                },
              ),
            )
          ],
        ),
      ),
    ));
  }
}
4

1 回答 1

0

I had to comment out a few lines but other than that I reproduced your exact code in DartPad. The code works as expected. The image also loaded as expected. Please see the code below :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MainOrderScreen(),
    );
  }
}

class MainOrderScreen extends StatefulWidget {
  MainOrderScreen({Key key}) : super(key: key);

  @override
  _MainOrderScreenState createState() => _MainOrderScreenState();
}

class _MainOrderScreenState extends State<MainOrderScreen> {
  //final _auth = FirebaseAuth.instance;
  //FirebaseFirestore _firestore = FirebaseFirestore.instance;

  String imageURL =   'https://images.ctfassets.net/3s5io6mnxfqz/6ZImCEzx6UuvuKaAiZEDDN/50479ee4a0902deb4eb1bab720ce248a/image1.jpg';

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Container(
          padding: EdgeInsets.symmetric(vertical: 20, horizontal: 20),
          child: Column(
            children: [
              Text(
                'currently serving:',
                //style: kTiffinzoTitle.copyWith(fontSize: 40),
              ),
              Image(
                image: imageURL != null ? NetworkImage(imageURL) : null,
                width: 200,
              ),
              Container(
                //decoration: kButtonStyle,
                child: FlatButton(
                  child: Text('Log out'),
                  onPressed: () {
                    //_auth.signOut();
                    Navigator.pop(context);
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
于 2020-11-15T19:45:48.290 回答