2

I am new to Flutter. I have a StatefulWidget with a default parameter, I have done my research and many have answered this question with vivid examples like izwebtechnologies and StackOverFlow I still cannot get my code to work. It gives me the error

Invalid argument(s)
The relevant error-causing widget was: 
Gallery file:///xxx/xxx/xxx/xxx/lib/main.dart:241:13

My line 214 looks like

  var images = new List<String>();
  .
  .
  .
  class NewGallery extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _scafoldkey = GlobalKey<ScaffoldState>();
    return Scaffold(
      key: _scafoldkey,
      backgroundColor: Colors.transparent,
     //Where the exception is occurring from main.dart:241:13
      body: Gallery(images),
    );
  }
}

My Gallery class

 class Gallery extends StatefulWidget {

  final List<String> images;
  const Gallery(this.images);

  @override
  State createState() => new _GalleryState();
 }

My Gallery State class

class _GalleryState extends State<Gallery> {
      int currentImageIndex;

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          backgroundColor: Colors.black,
          body: new Dismissible(
            //resizeDuration: null,
            onDismissed: (DismissDirection direction) {
              setState(() {
                currentImageIndex +=
                    direction == DismissDirection.endToStart ? 1 : -1;
                if (currentImageIndex < 0) {
                  currentImageIndex = widget.images.length - 1;
                } else if (currentImageIndex >= widget.images.length) {
                  currentImageIndex = 0;
                }
              });
            },
            key: new ValueKey(currentImageIndex),
            child: SizedBox.expand(
                child: Container(
                    color: Colors.black,
                    margin: const EdgeInsets.only(left: 0, top: 40),
                    child: FadeInImage.assetNetwork(
                        fit: BoxFit.fitWidth,
                        placeholder: "assets/images/placeholder.png",
                        image: widget.images.elementAt(currentImageIndex)))),
          ),
        );
      }
    }

Please note that I am very new to flutter and still do not understand a lot of stuff therefore my code might not be optimized.

Astonio
  • 635
  • 2
  • 8
  • 21

2 Answers2

1

You can copy paste run full code below
Wrong design you need to use ListView , currentImageIndex in this case will not work

working demo

enter image description here

full code

import 'package:flutter/material.dart';

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

//var images = new List<String>();
List<String> images = [
  "https://picsum.photos/250?image=9",
  "https://picsum.photos/250?image=9"
];

class NewGallery extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _scafoldkey = GlobalKey<ScaffoldState>();
    return Scaffold(
      key: _scafoldkey,
      backgroundColor: Colors.transparent,
      //Where the exception is occurring from main.dart:241:13
      body: Gallery(images),
    );
  }
}

class Gallery extends StatefulWidget {
  final List<String> images;
  Gallery(this.images);

  @override
  State createState() => new _GalleryState();
}

class _GalleryState extends State<Gallery> {
  int currentImageIndex;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.black,
      body: ListView.builder(
        itemCount: images.length,
        itemBuilder: (BuildContext context, int index) {
          return Dismissible(
            onDismissed: (DismissDirection direction) {
              setState(() {
                images.removeAt(index);
              });
            },
            secondaryBackground: Container(
              child: Center(
                child: Text(
                  'Delete',
                  style: TextStyle(color: Colors.white),
                ),
              ),
              color: Colors.red,
            ),
            background: Container(),
            child: Container(
                color: Colors.black,
                margin: const EdgeInsets.only(left: 0, top: 40),
                child: FadeInImage.assetNetwork(
                    fit: BoxFit.fitWidth,
                    placeholder: "assets/images/placeholder.png",
                    image: widget.images[index])),
            key: UniqueKey(),
            direction: DismissDirection.endToStart,
          );
        },
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: NewGallery(),
    );
  }
}
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
-1

Try removing the const in your Gallery constructor stateful widget.

class Gallery extends StatefulWidget {

  final List<String> images;
  Gallery(this.images);

  @override
  State createState() => new _GalleryState();
}
fadhli-sulaimi
  • 686
  • 12
  • 17