faker Dart Library

repository·master·Indexed 42 days ago

https://github.com/drager/faker

A Dart library for generating realistic fake data for testing and development, inspired by Python's 'faker' and Ruby's 'ffaker'. It provides generators for various data categories including person, internet, address, lorem, company, currency, date, food, and colors.

Tokens
6K
Snippets
36
Records
37
Agent score
53%

What's inside faker

  1. Get started with faker

    master

    To generate fake data in your Dart application, import package:faker/faker.dart and instantiate a new Faker object. You can then access various categories of fake data (such as internet, person, or lorem) through the Faker instance to generate specific types of information like emails, names, or sentences.

    import 'package:faker/faker.dart';
    
    main() {
      var faker = new Faker();
    
      faker.internet.email();
      // francisco_lebsack@buckridge.com
    
      faker.internet.ipv6Address();
      // 2450:a5bf:7855:8ce9:3693:58db:50bf:a105
    
      faker.internet.userName();
      // fiona-ward
    
      faker.person.name();
      // Fiona Ward
    
      faker.person.prefix();
      // Mrs.
    
      faker.person.suffix();
      // Sr.
      
      faker.lorem.sentence();
      // Nec nam aliquam sem et
    }
  2. Generate address data with the Address class

    master

    The Address class provides methods to generate various components of a physical address, including zip codes, cities, street names, and geographic locations. It relies on an associated Person instance to derive names for cities and streets.

    Commonly used methods include:

    • zipCode(): Returns a string matching patterns like ##### or #####-####.
    • city(): Generates a city name using prefixes, person names, and suffixes.
    • streetAddress(): Combines a building number and a street name.
    • state() and stateAbbreviation(): Returns US states and their corresponding abbreviations.
    • country(), countryCode(), and continent(): Returns geographic identifiers.
    // Assuming faker is an initialized instance of the main Faker class
    faker.address.zipCode();
    faker.address.city();
    faker.address.streetAddress();
    faker.address.state();
    faker.address.country();
  3. Generate RGB or Hex color strings with Color.rgbColor()

    master

    The rgbColor() method generates color strings in either Hex or CSS (rgb/rgba) formats.

    Parameters:

    • prefix (String): A prefix to prepend to hex colors (default: '#').
    • casing (Casing): Controls whether hex characters are Casing.lower or Casing.upper (default: Casing.lower).
    • format (Format): Determines the output format: Format.hex or Format.css (default: Format.hex).
    • includeAlpha (bool): If true, generates an 8-character hex code or an rgba() CSS string (default: false).
    // Hex color (default)
    faker.colors.rgbColor();
    
    // Hex color with custom prefix and uppercase
    faker.colors.rgbColor(prefix: '0x', casing: Casing.upper);
    
    // CSS rgb format
    faker.colors.rgbColor(format: Format.css);
    
    // CSS rgba format with alpha channel
    faker.colors.rgbColor(format: Format.css, includeAlpha: true);
    
    // Hex color with alpha channel
    faker.colors.rgbColor(format: Format.hex, includeAlpha: true);
  4. Generate data using patterns

    master

    You can generate formatted strings using patterns where # represents a random digit (0-9):

    • fromPattern(List pattern): Picks one pattern from the list and replaces # with random digits.
    • fromPatternToHex(List pattern): Picks one pattern from the list and replaces # with random hexadecimal characters (0-9, a-f).
    // Returns something like '12345-6789'
    String patternStr = random.fromPattern(['#####-####']);
    
    // Returns something like 'a1b2c-d3e4f'
    String hexStr = random.fromPatternToHex(['#####-####']);
  5. Use the deprecated image() method

    master

    The image() method is deprecated. It is recommended to use loremPicsum() instead for better control over image generation.

    Parameters:

    • width (int, default: 640)
    • height (int, default: 480)
    • keywords (List<String>, default: []): A list of categories (e.g., ['people', 'nature']) separated by commas.
    • random (bool, default: false): If true, ensures a different image is returned by using a random integer internally.
    faker.image.image(width: 1200, height: 900, keywords: ['people', 'nature'], random: true);
  6. Generate food data with the Food provider

    master

    The Food class provides methods to generate random food-related strings such as restaurants, dishes, and cuisines. These methods are typically accessed via a faker instance using the .food property.

    Available methods:

    • restaurant(): Returns a random restaurant name.
    • dish(): Returns a random dish name.
    • cuisine(): Returns a random cuisine type.
    // Generate a random restaurant
    faker.food.restaurant();
    
    // Generate a random dish
    faker.food.dish();
    
    // Generate a random cuisine
    faker.food.cuisine();
  7. Generate usernames and domain names

    master

    Generate identity and domain components:

    • userName(): Generates a lowercase username combining a first and last name with separators like _, ., or -.
    • domainName(): Generates a full domain name.
    • domainWord(): Generates a single word suitable for a domain name (sanitized to remove illegal characters).
    faker.internet.userName();
    faker.internet.domainName();
    faker.internet.domainWord();
  8. Generate currency data with the Currency class

    master

    The Currency class provides methods to generate random currency-related strings using a RandomGenerator and a CurrencyDataProvider. You can generate a random ISO currency code or a random currency name.

    Note: In a standard implementation, these are typically accessed via a top-level faker instance (e.g., faker.currency.code()).

    // Generate a random currency code (e.g., 'USD', 'EUR')
    String code = faker.currency.code();
    
    // Generate a random currency name (e.g., 'US Dollar')
    String name = faker.currency.name();
  9. Generate random date strings (month, year, time)

    master

    The Date class provides several methods to generate formatted strings representing dates and times:

    • month(): Returns a random month name (e.g., 'January').
    • year({int minYear, int maxYear}): Returns a random year as a string. It may optionally include a suffix like 'BC' or 'AD'.
    • time(): Returns a formatted time string. This can include a 12-hour clock suffix (AM/PM) and a random time zone (e.g., '3:42 PM PST').
    • justTime(): Returns a time string in 24-hour format using only digits (e.g., '23:52').
    faker.date.month();
    
    faker.date.year(minYear: 2000, maxYear: 2020);
    
    faker.date.time();
    
    faker.date.justTime();
  10. Generate job positions with `Company.position()`

    master

    Use the position() method to generate a randomized professional job title. Titles are composed of prefixes (like "Executive"), positions (like "Manager"), and areas (like "Finance").

    ```dart
      faker.company.position();