I want to add images path in SharedPreferences, but don't know to do

105 views Asked by At

I write a function which return List of Image and used in ListView, but I also want to make file for each and every image and stored the that file path in SharedPreferences.

import 'package:image/image.dart' as imglib;


Future<List<Image>> _getListOfImages() async {
    final dy = widget.dyCoordinate;
    // split image to parts
    List<imglib.Image> parts = List<imglib.Image>();
    for (int i = 1; i < dy.length; i++) {
      parts.add(
        imglib.copyCrop(
          src,
          0,
          (dy[i - 1] * aspectRatio).round(),
          properties.width,
          (dy[i] * aspectRatio).round() - (dy[i - 1] * aspectRatio).round(),
        ),
      );
    }

    List<Image> images = [];
    for (var img in parts) {
      images.add(Image.memory(imglib.encodeJpg(img)));
    }

    return images;
  }


@override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomSheet: AbsorbPointer(
          absorbing: _isEnable,
          child: Container(
            margin: EdgeInsets.all(0),
            alignment: Alignment.bottomCenter,
            height: 48,
            color: Colors.black87,
            child: ButtonBar(
              alignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              buttonMinWidth: MediaQuery.of(context).size.width / 2.2,
              children: <Widget>[
                FlatButton(
                  child: Text(
                    'Record Audio',
                    style: TextStyle(color: Colors.white70, fontSize: 16),
                  ),
                  // color: Colors.black45,
                  onPressed: () {
                    _showMyDialog();
                  },
                ),
                FlatButton(
                  child: Text(
                    'Select Audio',
                    style: TextStyle(color: Colors.white70, fontSize: 16),
                  ),
                  // color: Colors.black45,
                  onPressed: () {/** */},
                ),
              ],
            ),
          )),
      backgroundColor: Colors.black,
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.check),
      ),
      body: FutureBuilder(
        future: _getListOfImages(), // async work
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
            case ConnectionState.active:
              return Container(
                alignment: Alignment.center,
                child: CircularProgressIndicator(
                  backgroundColor: Colors.amber[50],
                ),
              );
              break;
            case ConnectionState.done:
              if (snapshot.hasError) {
                // return whatever you'd do for this case, probably an error
                return Container(
                  alignment: Alignment.center,
                  child: Text("Error: ${snapshot.error}"),
                );
              }
              var data = snapshot.data;
              return ListView.builder(
                reverse: false,
                itemBuilder: (_, int index) => ListTile(
                  dense: true,
                  onTap: () {
                    setState(() {
                      _isEnable = !_isEnable;
                    });
                  },
                  title: Container(
                    margin: const EdgeInsets.all(0),
                    child: data[index],
                  ),
                ),
                itemCount: data.length,
              );

              break;
          }
        },
      ),
    );
  }

Actually I want to implement cropper which split the image according to some coordinate and that coordinate store in widget.dyCoordinate and using 'image' library from copyCrop() after cropping image I stored in `parts' than encode that parts images of image library to Image of Flutter and return List of Image.

Your guy has any idea how to save every image some location on devices or make file after that stored it path in sharedPreferences

1

There are 1 answers

0
Abhishek Ghaskata On

If you want to store multiple image's paths then you have to use json.encode so that it will convert to string and then you can store it in shared preference.

SharedPreferences.setString(key, json.encode(value));

But better to use any local database like sqlite(https://pub.dev/packages/sqflite) or hive(https://pub.dev/packages/hive).