Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Store and retrieve colors in firestore flutter

I want to be able to store and use user selected text and background colors in firestore. I’ve tried storing them as strings and ints but the problem I run into is trying to use future< int> in Color() isn’t accepted and future< String> isn’t accepted in the "color:" parameter.

Right now I’m getting the hex value and storing it in firestore:

String hexColor() {
    late String c;
    if (BayTextColorController.selected.value == "Amber") {
      c = '0xFFFFAB40';
    } else if (BayTextColorController.selected.value == "Black") {
      c = '0xFF000000';
    }
    return c;
  }

How I’ve tried using ints:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    Future<int> bay56TextColor(DocumentSnapshot snapshot) async {
      String s = await snapshot.get('bay56TextColor');
      late int c = int.parse(s);
      return c;
    }

I’m trying to color the user submitted text from this future builder:

FutureBuilder<dynamic>(
       future: bay56(snapshot.data!),
       builder: (context, snapshot) {
                              
       if (snapshot.hasData) {
          return Text(
            ' ' + snapshot.data.toString() + ' ',
               style: TextStyle(
                  color: Colors.black,  //Color goes here
                  backgroundColor: Colors.white));  //and here
                              }

Is there a way to incorporate multiple futures into one future builder?

I should note that my text is from one field and I’m saving the colors in another field in the same document.

>Solution :

Store color in this format.

#FF6F00

Get color using this method

class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}

Example

color: HexColor(color code is here)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading