Skip to main content

Command Palette

Search for a command to run...

Console App in Dart that Shows Students' Data Using Maps (and its methods)

Published
4 min read
Console App in Dart that Shows Students' Data Using Maps (and its methods)
D

I am a Mobile App developer from Nigeria.

As usual, in the course of my internship with the ADA Software development program specifically during the fifth week, we were asked to create a student database for JSS1 using input from the Registrar of the school. As a beginner who had no idea about maps, I had to start my research on maps in dart for two days then, I proceeded to coding.

Task Overview

The principal of Evergreen was highly impressed by your previous solution and now requests that you build a new solution that will make the work of the registrar easier. But starting with jss1 students.

These are the requirements:

  • The registrar should be able to upload all the students names, date of birth, height, state of origin
  • They should be able to view this data in the console.

Task Objective:

  • Improve your logic skills
  • Help you experiment with maps in dart and it's methods.
  • Solidify the basics you've been practicing with.

Steps In Achieving this Code

  1. I imported dart:io and dart:core which we will use to input student data and create map that is needed respectively. 2. I created an empty list and called it JSS1 then, created a function addStudent() to store the different data inputs from the registrar. 3. The data inputs included three strings and two integers hence, I created variables for the different data; name, date of birth, age, height, state of origin and used stdin.readLineSync() which can be found in dart:io, to pass either as a string or converting to an integer . To convert integer inputs using stdin.readLineSync, I used int.parse(stdin.readLineSync).
  2. I created a map with keys and values and made sure to call the variables created above into the values.
  3. I finally used the .add method in map to add each Student data created from the input into our empty list [] just like this, JSS1.add(Student).
  4. In line 51, I created another function called printStudents() and created a nested for...in loop that will take the data in 5 above and print out each keys and values.
  5. In the main function (line 59), I called the addStudent() function then created a while loop that always return true. I want this loop to ask the registrar to press 1 to continue adding student's name (i.e addstudent function) or press 0 to view the data of the student(s) he had already inputted (printStudent function)which will come out as a map because of what I created in 6 above.
  6. To enable this loop, I introduced the if/else statement.

Code

import 'dart:io';
import 'dart:core';

// creating an empty list

final JSS1 = [];

// A function that prompts registrar to input student data.
void addStudent() {
  print(
      "Welcome, this is the JSS1 students database, please enter the student's name.");
  final String name = stdin.readLineSync()!;

  print("Enter student's age.");
  final int age = int.parse(stdin.readLineSync()!);

  print("Enter student's date of birth.");
  final String dateOfBirth = stdin.readLineSync()!;

  print("Enter height in centimeters.");
  final int height = int.parse(stdin.readLineSync()!);

  print("Enter student's state of origin.");
  final String stateOfOrigin = stdin.readLineSync()!;

// A map that accepts the above student's data
  var Student = {
    "name": name,
    "age": age,
    "dateOfBirth": dateOfBirth,
    "height": height,
    "stateOfOrigin": stateOfOrigin,
  };
  JSS1.add(Student);


}
// function that can be called in the main function to print student name(s).

void printStudents() {
  for (var Student in JSS1) {
    for(var singleStudent in Student.entries) {
      print('${singleStudent.key}: ${singleStudent.value}');
    }
  }
}

void main() {
// call the addStudent function so it will be the first thing to show on the console.
addStudent();
  // A while loop that always returns true
  while(true) {
    print("Enter 1 to add another student's name or enter 0 to view list of students already in database.");
  final int userInput = int.parse(stdin.readLineSync()!);
    if(userInput == 1){
    addStudent();
    }else if(userInput == 0){
      printStudents();
      break;
    }else{
      print('Invalid input, try again.');
    }
  }

}

Challenges Faced

In Nigeria, we would rephrase this heading as "Shege seen successfully" because it was not easy to understand the task at first. I kept asking our facilitator and chief distributor of this "shege", Ms. Sophia Ahuoyiza questions so that I could understand what she wanted us to achieve. My developer friends were not left behind regardless of their tracks and some of them were eager to help as well. Finally, I achieved this with the help of God, my friends, my facilitator, and track colleagues.

Remember that Giving up is not an Option!**

More from this blog

TechDEBBY

11 posts

I am an enthusiast, curious about everything. A Junior front-end Developer and aspiring Mobile Developer who is keen on creating mind-blowing tech inventions that would help the community.