Home > Blockchain >  How to separate data into different databases?
How to separate data into different databases?

Time:11-14

I have a database where I want to add and store information about a particular destination or activity. In the insert information page, I have a section where user can select either destination or activity as its category. However, all these are being stored into one database but I want to have different ones for each.

This is the current code that adds them into one database:

  void addDestOrAct(String name, String details, var category) {
    String key = destdatabaseref
        .child('Database')
        .child('DestinationsandActivities')
        .push()
        .key;
    destdatabaseref
        .child('Database')
        .child('DestinationsandActivities')
        .child(name)
        .set({
      'id': key,
      'name': name,
      'description': details,
      'category': category
    });
    nameController.clear();
    descriptionController.clear();

    Fluttertoast.showToast(
        timeInSecForIosWeb: 4,
        gravity: ToastGravity.CENTER,
        msg: "It has been added!");

    Navigator.pop(context);
  }

I want to put something like if (category = 'Activity') then add into destdatabaseref.child('Database').child('Activities') instead of .child('DestinationsandActivities').

Selection of category and inserting data code:

                Padding(
                  padding: const EdgeInsets.only(right: 290),
                  child: Text(
                    'Category :',
                    style: TextStyle(fontSize: 15, color: Colors.grey),
                  ),
                ),
                SizedBox(height: 13),
                Container(
                  height: 40,
                  child: ListView(
                    scrollDirection: Axis.horizontal,
                    children: [
                      _categoryType('Destination'),
                      SizedBox(width: 10),
                      _categoryType('Activity'),
                    ],
                  ),
                ),
                SizedBox(height: 40),
                ElevatedButton(
                    style: ElevatedButton.styleFrom(
                        primary: Color(0xFF3d5a89),
                        padding:
                            EdgeInsets.symmetric(horizontal: 45, vertical: 10),
                        textStyle: TextStyle(
                            fontWeight: FontWeight.w500,
                            color: Colors.white,
                            fontSize: 15)),
                    onPressed: () {
                      if (_formKey.currentState!.validate()) {
                        if (nameController.text.isNotEmpty &&
                            descriptionController.text.isNotEmpty) {
                          addDestOrAct(nameController.text,
                              descriptionController.text, _categorySelected);
                        } else
                          return null;
                      }
                    },
                    child: Text('Add')
                    )

Category type widget code:

Widget _categoryType(String title) {
    return InkWell(
      child: Container(
        height: 70,
        width: 120,
        decoration: BoxDecoration(
          color: _categorySelected == title
              ? Color(0xFF5a893d)
              : Color(0xFF3d5a89),
          borderRadius: BorderRadius.circular(15),
        ),
        child: Center(
          child: Text(
            title,
            style: TextStyle(fontSize: 13, color: Colors.white),
          ),
        ),
      ),
      onTap: () {
        setState(() {
          _categorySelected = title;
        });
      },
    );
  }

How can I can add information into different databases based on category? I have the database 'DestinationsandActivities' for now but would really prefer it to be 'Destinations' and 'Activities'.

CodePudding user response:

Thanks to @Frank van Puffelen, it was simply :

    if (category == 'Activity') {
      //code here
      });
    }

    if (category == 'Destination') {
      //code here
      });
    }
  • Related