在 Flutter 中,我在从互联网上获取图像时遇到问题


class Question {
final String question;
final Image imageName;
final Answer correctAnswer;
Question(
{required this.question,
required this.imageName,
required this.correctAnswer});
}

我创建了一个名为Question的类,并添加了我想在其中使用的功能

class QuestionResult {
static List<QuestionResult> defaultQuestions = [
QuestionResult(
question: Question(
question:
"Some Question",
correctAnswer: Answer.yes,
imageName: Image.network(
'https://images.pexels.com/photos/288093/pexels-photo-288093.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'))),
}

然后我在屏幕上向用户展示一个问题,让他回答。我还从互联网上获取了这个问题的背景图像。

final List<QuestionResult> _questions =
QuestionResult.getRandomDefaultQuestionsShuffled();
int _currentQuestionIndex = 0;
image: AssetImage(
"${widget._questions.elementAt(_currentQuestionIndex).question.imageName}"),

在我想显示问题和图像的页面上,我试图获得我用这样的代码分配给问题的图像。

我把图像作为Image.Network((显示给用户。然而,在这里,我无法建立连接,并试图用AssetImage((调用它。当然,它没有显示图像,因为它找不到任何资产文件夹。如何在此处建立Image.network连接?

对于从互联网上获取的图像,您可以使用NetworkImage而不是AssetImage

像这样:

image: DecorationImage(image: NetworkImage('www.google.com'),),

最新更新