Home > Net >  Flutter Images load slow
Flutter Images load slow

Time:10-28

I have added a Video to visualize my problem better: https://streamable.com/wdhna2

The problem is that my images load slowly, even I added a precache method and a splash screen. On first boot, the problem isn't that big, maybe because of the precache. here I'm implementing the precache method:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    loadImage(context);  <--- Method call
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);

and here is my precache method (just a snipped, not for all pictures):

import 'package:flutter/material.dart';

void loadImage(context){
  precacheImage(const AssetImage("assets/widgetsImages/bmi.jpg"), context);
  precacheImage(const AssetImage("assets/widgetsImages/calc.webp"), context);
  precacheImage(const AssetImage("assets/widgetsImages/chat.webp"), context);
}

I also tried to add a splash screen with the flutter_native_splash package, but this wouldn't fix the problem that the images load slowly after I close and reopen the app, without clearing my RAM. Anyway, here is the code for that:

Future<void> main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

and the end of the splashscreen when the first window opens:

class _NutriWidgetState extends State<NutriWidget> {

  @override
  void initState() {
    super.initState();
    FlutterNativeSplash.remove();
  }

Do you have an idea how I could load the images faster? Do I do something wrong?

CodePudding user response:

try image.assets("assets/widgetsImages/bmi.jpg")

CodePudding user response:

think you can not call directly into the widget builder you have to wrap it into Scaffolds

or return your method not call this

return loadImage(context)

  • Related