首页 > 解决方案 > How to solve flutter "Infinity or NaN toInt" on some images

问题描述

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.

标签: flutterflutter-layoutflutter-image

解决方案


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.


推荐阅读