1

I have a simple project that should just display an image. I should mention that am very new to flutter.

Below is the main.dart

import 'package:flutter/material.dart';
import 'package:flutter_shoe/ColorHolder.dart';
import 'package:flutter_shoe/home_page.dart';
import 'package:flutter_shoe/profile_page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: primaryColor,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(),
    );
  }
}

And here is the HomepageClass

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Icon(Icons.menu),
        title: Text("Home"),
        centerTitle: true,
        actions: [
          Icon(Icons.circle_notifications),
        ],
      ),
      body: Container(
        child: Image.asset(
          "assets/imgs/curved.png",
          // "assets/imgs/doc.jpg",
          width: 500,
          height: 200,
        ),
      ),
    );
  }
}

The above code is giving me the error

Unsupported operation: Infinity or NaN toInt

The error points to the line that contains the image asset.

I have tried using several different images and it is working well with some images and gives error on other images. I need it to always work with all images.

A sample working image has dimensions 626 by 626 and is jpg A sample non working image has dimension 1080 by 768 and is png

I have tried replacing container with Expandable. I have appended height and width value to the container. Still not working I have changed the width and height of image to double.maxFinite. Not working.

I don't find anything on their docs that is helping.

Someone help me know where I am going wrong.

4

3 回答 3

7

I solved it. Turns out that I should not use hot-reload after adding a new assets. All I needed was to stop the project and re-run it afresh.

于 2020-11-02T03:00:08.557 回答
1

Did you place your assets in the pubspec.yaml file?

flutter:
  assets:
    - assets/imgs/
于 2020-11-01T15:56:51.467 回答
1

I could not reproduce your errors, I will suggest you to try the following code :


body: SingleChildScrollView(
        child: Container(
           child: Image.asset("assets/imgs/curved.png",
                fit: BoxFit.cover,
                width: 500,
                height: 200,
             ),
          ),
       ),

Does using Image.network also give you an error. Please check if the following image gives error:

Image.network("https://placeimg.com/1080/768/any",
        width: 500, height: 200)
于 2020-11-01T16:22:26.680 回答